如何在excel应用程序中从access vba中设置警告

我打开excel工作簿访问后,写入内容从访问Excel,我使用xlobj.save保存工作簿。 Excel应用程序正在给出一些警告说,这个工作簿已经存在,你要replace它。 如何禁止访问这些警告。

我正在使用DoCmd.SetWarnings,但不工作。

这是我的代码

Public Sub sCopyResultstoexcel(conSHT_NAME As Variant, conWKB_NAME As Variant, qrytable As String) 'Copy records to first 20000 rows 'in an existing Excel Workbook and worksheet Dim objXL As Excel.Application Dim objWkb As Excel.Workbook Dim objSht As Excel.Worksheet Dim db As Database Dim rs As Recordset Dim rs_Attribute As Recordset Dim intLastCol As Integer Const conMAX_ROWS = 20000 Set db = CurrentDb Set objXL = New Excel.Application Set rs = db.OpenRecordset(qrytable, dbOpenSnapshot) With objXL .Visible = True DoCmd.SetWarnings off Set objWkb = .Workbooks.Open(conWKB_NAME) On Error Resume Next Set objSht = objWkb.Worksheets(conSHT_NAME) If Not Err.Number = 0 Then Set objSht = objWkb.Worksheets.Add objSht.Name = conSHT_NAME End If Err.Clear On Error GoTo 0 intLastCol = objSht.UsedRange.Columns.Count With objSht .Cells.ClearContents DoCmd.SetWarnings off .Range(.Cells(2, 1), .Cells(conMAX_ROWS, _ intLastCol)).CopyFromRecordset rs .Range(.Cells(1, 1), _ .Cells(1, rs.Fields.Count)).Font.Bold = True .Range(.Cells(1, 1), _ .Cells(1, rs.Fields.Count)).WrapText = False 'Formatting With objSht.Range("A1:CP1") .HorizontalAlignment = xlCenter .ColumnWidth = "8" .Font.Italic = False .Font.Bold = True .EntireColumn.ColumnWidth = 15 End With 'Adding fields With rs For i = 1 To .Fields.Count objSht.Cells(1, i) = .Fields(i - 1).Name Next i DoCmd.SetWarnings off objWkb.Save End With End With End With 'objWkb.Close 'objXL.Quit Set objSht = Nothing Set objWkb = Nothing Set objXL = Nothing Set rs = Nothing Set db = Nothing End Sub 

我尝试使用VBA打开只读/已打开的工作簿时遇到类似的问题。

在你的路线后面:

 Set objXL = New Excel.Application 

 objXL.DisplayAlerts = False objXL.AskToUpdateLinks = False objXL.EnableEvents = False 

为了避免现有文档的保存,您可以暂时将其保存在某处,然后强制将其复制。 那种野蛮的,但作品…

 Set FSO = CreateObject("Scripting.FileSystemObject") FSO.CopyFile SourceFile, DestinationFile, True FSO.DeleteFile SourceFile 

其中SourceFile和DestinationFile是具有文件名的path。

  1. 检查文件是否已经存在(使用Dir命令)。
  2. 如果是,请删除它。
  3. 保存你的文件。