我尝试使用
Application.DisplayAlerts = wdAlertsNone
或 Application.DisplayAlerts = False
来避免在保存 Word 文档之前出现 Word 弹出消息。
正在保存的文档包含跟踪更改是否继续保存?
Private Sub CreateReportButton_Click()
Dim objDocA As Word.Document
Dim objDocB As Word.Document
Dim objDocC As Word.Document
Dim objFSO As Scripting.FileSystemObject
Dim objFolderA As Scripting.Folder
Dim objFolderB As Scripting.Folder
Dim objFolderC As Scripting.Folder
Dim colFilesA As Scripting.Files
Dim objFileA As Scripting.File
Dim i As Integer
Dim j As Integer
Set objFSO = New FileSystemObject
Set objFolderA = objFSO.GetFolder(ChooseFolder("Choose the folder with the original documents", ThisDocument.Path))
Set objFolderB = objFSO.GetFolder(ChooseFolder("Choose the folder with revised documents", ThisDocument.Path))
Set objFolderC = objFSO.GetFolder(ChooseFolder("Choose the folder for the comparisons documents", ThisDocument.Path))
Set colFilesA = objFolderA.Files
For Each objFileA In colFilesA
If objFileA.Name Like "*.docx" Then
Set objDocA = Documents.Open(objFolderA.Path & "\" & objFileA.Name)
Set objDocB = Documents.Open(objFolderB.Path & "\" & objFileA.Name)
Set objDocC = Application.CompareDocuments( _
OriginalDocument:=objDocA, _
RevisedDocument:=objDocB, _
Destination:=wdCompareDestinationNew)
objDocA.Close
objDocB.Close
On Error Resume Next
Kill objFolderC.Path & "\" & objFileA.Name
On Error GoTo 0
'Turn off DisplayAlerts
Application.DisplayAlerts = wdAlertsNone
objDocC.SaveAs FileName:=objFolderC.Path & "\" & objFileA.Name
objDocC.Close SaveChanges:=False
End If
Next objFileA
End Sub
显然这取决于 Office 的版本,在 2013 年,需要转到应用程序的信任中心区域(文件 > 选项 > 信任中心 > 信任中心设置 > 隐私选项)并取消选中“打印、保存或打印前预热”选项发送包含跟踪更改或评论的文件”。完成后,文件将与 Word 的任何消息一起保存
如果您需要保留这些设置,那么出于代码的目的,您可以使用以下内容:
Options.WarnBeforeSavingPrintingSendingMarkup = False
ActiveDocument.Save
Options.WarnBeforeSavingPrintingSendingMarkup = True
或者,为了使可能不使用该设置的系统获得更大的灵活性:
Dim bWarn as Boolean
bWarn = Options.WarnBeforeSavingPrintingSendingMarkup
Options.WarnBeforeSavingPrintingSendingMarkup = False
ActiveDocument.Save
Options.WarnBeforeSavingPrintingSendingMarkup = bWarn
如果 WarnBeforeSavingPrintingSendingMarkup 不适合您,请尝试在 wdDoc 之后添加它。
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open(fileName:=pdfFile)
wdDoc.Options.WarnBeforeSavingPrintingSendingMarkup = False
它不是这样工作的:
wdApp.Options.WarnBeforeSavingPrintingSendingMarkup = False