如果没有VB.NET中的“真的想要覆盖”对话框,如何覆盖其他Excel文件

时间:2022-12-29 21:15:43

How can I save an Excel-file, which I'd edit with VB.NE, to a file, which already exists? Evertime there is a dialog: File already exists. Do you really want to overwrite? YES|NO|Abort

如何将我用VB.NE编辑的Excel文件保存到已存在的文件中? Evertime有一个对话框:文件已经存在。你真的想覆盖吗? YES | NO |中止

How can I overwrite without this dialog?

如果没有此对话框,我该怎么覆盖?

6 个解决方案

#1


7  

You should have a look at setting

你应该看看设置

DisplayAlerts=false

Application.DisplayAlerts Property

Application.DisplayAlerts属性

Set this property to False if you don’t want to be disturbed by prompts and alert messages while a program is running; any time a message requires a response, Microsoft Excel chooses the default response.

如果您不希望在程序运行时被提示和警报消息干扰,请将此属性设置为False;每当邮件需要响应时,Microsoft Excel都会选择默认响应。

Just remember to reset it to true once you are done.

一旦完成,请记住将其重置为true。

We currently do it as follows

我们目前这样做如下

object m_objOpt = Missing.Value;
m_Workbook.Application.DisplayAlerts = false;
m_Workbook.SaveAs(  fileName, m_objOpt, m_objOpt, 
                    m_objOpt, m_objOpt, m_objOpt, 
                    XlSaveAsAccessMode.xlNoChange, 
                    XlSaveConflictResolution.xlLocalSessionChanges, 
                    m_objOpt, m_objOpt, m_objOpt, m_objOpt);

#2


2  

 Dim xlWorkBook As Excel.Workbook
 Dim xlWorkSheet As Excel.Worksheet
 Dim misValue As Object = System.Reflection.Missing.Value

 xlWorkBook.SaveAs(<YourFileName>, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue,   misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
 xlWorkBook.Close(True, misValue, misValue)

#3


1  

Couldn't you try to delete the file first, before overwriting it?

在覆盖之前,你不能先尝试删除文件吗?

#4


1  

There is a property within the SaveFileDialog class, called OverwritePrompt, set that to false.

SaveFileDialog类中有一个名为OverwritePrompt的属性,将其设置为false。

Hope this helps.

希望这可以帮助。

#5


0  

I solved the problem setting the third parameter of xlApp.Workbooks.Open to false when you create the workbook. That is the readonly argument and if it is set to true it will ask for saving file.

我解决了在创建工作簿时将xlApp.Workbooks.Open的第三个参数设置为false的问题。这是readonly参数,如果设置为true,它将要求保存文件。

Sub ExcelMacroExec2()
    Dim xlApp, xlBook

    Set xlApp = CreateObject("Excel.Application")
    xlApp.DisplayAlerts = False

    Set xlBook = xlApp.Workbooks.Open("C:\Users\A\myFile.xlsm", 0, False)
    xlApp.Run "Macro1"   

    xlApp.Save 
    xlBook.Close false
    xlApp.Quit   

    Set xlApp = Nothing
    set xlBook = Nothing
End Sub

#6


0  

I resolve this issue by using the Object.Save() method instead. I tried to use the SaveAs method even with the DisplayAlerts = $false but it still kept giving me the overwrite warning.

我通过使用Object.Save()方法来解决此问题。我尝试使用SaveAs方法,即使DisplayAlerts = $ false,但它仍然一直给我覆盖警告。

Code Snippet Below:

代码片段如下:

# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $true
# Override warning messages about existing file
$objExcel.DisplayAlerts = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
# Pause execution to allow data refresh to occur (about 5 minutes to be safe)
Start-Sleep -s 300
# Now the Save the file After Refreshing (need to add a pause for about 5 minutes)
$WorkBook.Save()
# Wait while saving before closing excel object
Start-Sleep -s 30
# Now close the workbook

#1


7  

You should have a look at setting

你应该看看设置

DisplayAlerts=false

Application.DisplayAlerts Property

Application.DisplayAlerts属性

Set this property to False if you don’t want to be disturbed by prompts and alert messages while a program is running; any time a message requires a response, Microsoft Excel chooses the default response.

如果您不希望在程序运行时被提示和警报消息干扰,请将此属性设置为False;每当邮件需要响应时,Microsoft Excel都会选择默认响应。

Just remember to reset it to true once you are done.

一旦完成,请记住将其重置为true。

We currently do it as follows

我们目前这样做如下

object m_objOpt = Missing.Value;
m_Workbook.Application.DisplayAlerts = false;
m_Workbook.SaveAs(  fileName, m_objOpt, m_objOpt, 
                    m_objOpt, m_objOpt, m_objOpt, 
                    XlSaveAsAccessMode.xlNoChange, 
                    XlSaveConflictResolution.xlLocalSessionChanges, 
                    m_objOpt, m_objOpt, m_objOpt, m_objOpt);

#2


2  

 Dim xlWorkBook As Excel.Workbook
 Dim xlWorkSheet As Excel.Worksheet
 Dim misValue As Object = System.Reflection.Missing.Value

 xlWorkBook.SaveAs(<YourFileName>, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue,   misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
 xlWorkBook.Close(True, misValue, misValue)

#3


1  

Couldn't you try to delete the file first, before overwriting it?

在覆盖之前,你不能先尝试删除文件吗?

#4


1  

There is a property within the SaveFileDialog class, called OverwritePrompt, set that to false.

SaveFileDialog类中有一个名为OverwritePrompt的属性,将其设置为false。

Hope this helps.

希望这可以帮助。

#5


0  

I solved the problem setting the third parameter of xlApp.Workbooks.Open to false when you create the workbook. That is the readonly argument and if it is set to true it will ask for saving file.

我解决了在创建工作簿时将xlApp.Workbooks.Open的第三个参数设置为false的问题。这是readonly参数,如果设置为true,它将要求保存文件。

Sub ExcelMacroExec2()
    Dim xlApp, xlBook

    Set xlApp = CreateObject("Excel.Application")
    xlApp.DisplayAlerts = False

    Set xlBook = xlApp.Workbooks.Open("C:\Users\A\myFile.xlsm", 0, False)
    xlApp.Run "Macro1"   

    xlApp.Save 
    xlBook.Close false
    xlApp.Quit   

    Set xlApp = Nothing
    set xlBook = Nothing
End Sub

#6


0  

I resolve this issue by using the Object.Save() method instead. I tried to use the SaveAs method even with the DisplayAlerts = $false but it still kept giving me the overwrite warning.

我通过使用Object.Save()方法来解决此问题。我尝试使用SaveAs方法,即使DisplayAlerts = $ false,但它仍然一直给我覆盖警告。

Code Snippet Below:

代码片段如下:

# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $true
# Override warning messages about existing file
$objExcel.DisplayAlerts = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
# Pause execution to allow data refresh to occur (about 5 minutes to be safe)
Start-Sleep -s 300
# Now the Save the file After Refreshing (need to add a pause for about 5 minutes)
$WorkBook.Save()
# Wait while saving before closing excel object
Start-Sleep -s 30
# Now close the workbook