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

时间:2010-02-11 11:11:24

标签: vb.net excel

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

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

7 个答案:

答案 0 :(得分:7)

你应该看看设置

DisplayAlerts=false

Application.DisplayAlerts Property

  

如果您将此属性设置为False   不想被提示打扰   和程序时的警报消息   运行;任何时候消息需要一个   响应,Microsoft Excel选择   默认回复。

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

我们目前按以下方式进行

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

答案 1 :(得分: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)

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

希望这有帮助。

答案 4 :(得分:0)

我解决了在创建工作簿时将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

答案 5 :(得分:0)

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

下面的代码片段:

# 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

答案 6 :(得分:0)

在保存命令之前使用 (Application.DisplayAlerts = False) 和之后使用 (Application.DisplayAlerts = True) 避免对话框:

Workbooks.Open "C:\XML\baseline\RTWP\Analisis.xlsb"
Windows("Analisis.xlsb").Activate
Sheets("Data").Select
Cells.Select
Selection.ClearContents
Workbooks.Open "C:\XML\baseline\RTWP\output\totemplate.csv"
Windows("totemplate.csv").Activate
Sheets("totemplate").Select
Cells.Select
Selection.Copy
Windows("Analisis.xlsb").Activate
Range("A1").Select
ActiveSheet.Paste
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs fileName:="C:\XML\baseline\RTWP\Analisis.xlsb", FileFormat:=xlExcel12, CreateBackup:=False
Application.DisplayAlerts = True
ActiveWorkbook.Close SaveChanges:=False
Windows("totemplate.csv").Activate
ActiveWorkbook.Close SaveChanges:=False