简体   繁体   English

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

[英]How can I overwrite an other Excel-file without the Dialog “Really want to overwrite” in VB.NET

How can I save an Excel-file, which I'd edit with VB.NE, to a file, which already exists? 如何将我用VB.NE编辑的Excel文件保存到已存在的文件中? Evertime there is a dialog: File already exists. Evertime有一个对话框:文件已经存在。 Do you really want to overwrite? 你真的想覆盖吗? YES|NO|Abort YES | NO |中止

How can I overwrite without this dialog? 没有这个对话框我怎么能覆盖?

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; 如果您不希望在程序运行时被提示和警报消息干扰,请将此属性设置为False; any time a message requires a response, Microsoft Excel chooses the default response. 每当邮件需要响应时,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);
  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)

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

There is a property within the SaveFileDialog class, called OverwritePrompt , set that to false. SaveFileDialog类中有一个名为OverwritePromptSaveFileDialog ,将其设置为false。

Hope this helps. 希望这可以帮助。

I solved the problem setting the third parameter of xlApp.Workbooks.Open to false when you create the workbook. 我解决了在创建工作簿时将xlApp.Workbooks.Open的第三个参数设置为false的问题。 That is the readonly argument and if it is set to true it will ask for saving file. 这是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

I resolve this issue by using the Object.Save() method instead. 我通过使用Object.Save()方法来解决此问题。 I tried to use the SaveAs method even with the DisplayAlerts = $false but it still kept giving me the overwrite warning. 我尝试使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM