用于保存word文档和DisplayAlerts的Word vba脚本

时间:2017-12-18 10:32:13

标签: word-vba

我尝试在我的脚本中使用Application.DisplayAlerts = wdAlertsNoneApplication.DisplayAlerts = False来避免使用Word弹出消息" 正在保存的文档包含跟踪更改继续保存&#34?;就在保存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

你能帮我解决一下这个问题吗?

提前感谢您的帮助

2 个答案:

答案 0 :(得分:0)

显然,这取决于办公室的版本,2013年有必要转到应用程序的信任中心区域(文件>选项>信任中心>信任中心设置>隐私选项)和取消选中"打印前保暖,保存或发送包含已跟踪更改或评论的文件"。执行该操作后,文件将与Word

的任何消息一起保存

答案 1 :(得分:0)

如果您需要保留这些设置,那么就您的代码而言,您可以使用以下内容:

Options.WarnBeforeSavingPrintingSendingMarkup = False
ActiveDocument.Save
Options.WarnBeforeSavingPrintingSendingMarkup = True

或者,对于可能不使用该设置的系统,以获得更大的灵活性:

Dim bWarn as Boolean
bWarn = Options.WarnBeforeSavingPrintingSendingMarkup
Options.WarnBeforeSavingPrintingSendingMarkup = False
ActiveDocument.Save
Options.WarnBeforeSavingPrintingSendingMarkup = bWarn