How can I overwrite another Excel file without the "Actually want to overwrite" dialog in VB.NET

How to save an Excel file that I will edit with VB.NE to a file that already exists? Evertime has a dialog: The file already exists. Are you sure you want to overwrite? YES | NO | Abort

How can I overwrite without this dialog?

+3
source share
6 answers

You should take a look at the setting

DisplayAlerts=false

Application.DisplayAlerts Property

Set this property to False if you do not want to be bothered by prompts and warning messages when Run; Anytime an answer is required, Microsoft Excel selects the default answer.

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

SaveFileDialog , OverwritePrompt, false.

, .

+1

I solved the problem by setting the third parameter xlApp.Workbooks.Open to false when you create the book. This is a readonly argument, and if set to true, it will ask you to save the file.

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
0
source

I solve this problem using the Object.Save () method. I tried to use the SaveAs method even with DisplayAlerts = $ false, but it still gave me an overwrite warning.

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
0
source

Source: https://habr.com/ru/post/1732335/


All Articles