74

在这一段代码中,Excel总是提示:“文件已经存在,你要覆盖吗?”

Application.DisplayAlerts = False
Set xls = CreateObject("Excel.Application")
Set wb = xls.Workbooks.Add
fullFilePath = importFolderPath & "\" & "A.xlsx"

wb.SaveAs fullFilePath, AccessMode:=xlExclusive, ConflictResolution:=True   

wb.Close(True)

如果我有,为什么db.SaveAs总是提示我覆盖现有文件DisplayAlerts = False

4

3 回答 3

95

隐藏提示集xls.DisplayAlerts = False

ConflictResolution不是truefalse属性,它应该是xlLocalSessionChanges

请注意,这与显示覆盖提示无关!

Set xls = CreateObject("Excel.Application")    
xls.DisplayAlerts = False
Set wb = xls.Workbooks.Add
fullFilePath = importFolderPath & "\" & "A.xlsx"

wb.SaveAs fullFilePath, AccessMode:=xlExclusive,ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges    
wb.Close (True)
于 2013-01-31T20:42:50.330 回答
26

我建议在执行 SaveAs 之前,删除该文件(如果存在)。

If Dir("f:ull\path\with\filename.xls") <> "" Then
    Kill "f:ull\path\with\filename.xls"
End If

这比关闭和打开 DisplayAlerts 更容易,而且如果 DisplayAlerts 由于代码崩溃而仍然关闭,如果您在同一会话中使用 Excel 可能会导致问题。

于 2018-07-05T10:45:44.463 回答
10

分裂意见分歧

我更喜欢:

   xls.DisplayAlerts = False    
   wb.SaveAs fullFilePath, AccessMode:=xlExclusive, ConflictResolution:=xlLocalSessionChanges
   xls.DisplayAlerts = True
于 2019-03-16T07:51:51.093 回答