如何在 VB.NET 中没有对话框“真的想覆盖”的情况下覆盖其他 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2243995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:40:58  来源:igfitidea点击:

How can I overwrite an other Excel-file without the Dialog "Really want to overwrite" in VB.NET

vb.netexcel

提问by rakete

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 文件保存到已存在的文件中?总是有一个对话框:文件已经存在。你真的要覆盖吗?是|否|中止

How can I overwrite without this dialog?

如果没有此对话框,我如何覆盖?

回答by Adriaan Stander

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);

回答by sansalk

 Dim xlWorkBook As Excel.Workbook
 Dim xlWorkSheet As Excel.Worksheet
 Dim misValue As Object = System.Reflection.Missing.Value
 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)

回答by Prof. Falken contract breached

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

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

回答by t0mm13b

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

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

Hope this helps.

希望这可以帮助。

回答by Nono

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

回答by rdavidson

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() 方法解决了这个问题。即使使用 DisplayAlerts = $false 我也尝试使用 SaveAs 方法,但它仍然不断给我覆盖警告。

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