【问题标题】:Comparing Word documents and creating a new document with track changes比较 Word 文档并创建带有跟踪更改的新文档
【发布时间】:2021-04-09 07:20:22
【问题描述】:

我正在尝试在 excel 文档中创建一个 vba 脚本,以便比较 Word 文档版本并创建具有差异的摘要 Word 文档(跟踪更改)。

这是我的脚本:

Option Explicit
Private Sub ButtonSummaryReport_Click()
    'Initialize the progressbar and the label
    Dim k As Integer
    Dim filesNumber As Integer
    
    Dim i As Integer
    Dim j As Integer
    Dim objFolderAPath As String
    Dim objFolderBPath As String
    Dim objFolderCPath As String
    
    Dim FileName As String
    Dim WDApp As Object 'Word.Application
    Dim WDDocA As Object 'Word.Document
    Dim WDDocB As Object 'Word.Document
    Dim WDDocC As Object 'Word.Document
    
    'Declare variable
    Dim objFSOA As Object
    Dim objFSOB As Object
    Dim objFSOC As Object
    Dim objFolderA As Object
    Dim objFolderB As Object
    Dim objFolderC As Object
    Dim colFilesA As Object
    Dim objFileA As Object
    Dim PathFileA As String
    Dim PathFileB As String
    Dim PathFileC As String
    
    Dim wordapp
    
    k = 0
    Me.LabelSummaryReport.Caption = "Please wait..."
    Me.ProgressBarSummaryReport.Value = k
    
 
    'Create an instance of the FileSystemObject
    Set objFSOA = CreateObject("Scripting.FileSystemObject")
    Set objFSOB = CreateObject("Scripting.FileSystemObject")
    Set objFSOC = CreateObject("Scripting.FileSystemObject")
    
    'Select the path for the 3 folders
    Set objFolderA = objFSOA.GetFolder(ChooseFolder("Choose the folder with the original documents"))
    objFolderAPath = objFolderA.Path
    Debug.Print objFolderAPath
    
    Set objFolderB = objFSOB.GetFolder(ChooseFolder("Choose the folder with revised documents"))
    objFolderBPath = objFolderB.Path
    Debug.Print objFolderBPath
    
    Set objFolderC = objFSOC.GetFolder(ChooseFolder("Choose the folder for the comparisons documents"))
    objFolderCPath = objFolderC.Path
    Debug.Print objFolderCPath


    Set colFilesA = CreateObject("Scripting.FileSystemObject")
    Set objFileA = CreateObject("Scripting.FileSystemObject")
    
    Set colFilesA = objFolderA.Files
    
    'Turn off DisplayAlerts
    Application.DisplayAlerts = wdAlertsNone
 
    'Number of files in the folder
    filesNumber = objFolderA.Files.Count
       
    Me.LabelSummaryReport.Caption = "The comparison process starts..."
    For Each objFileA In colFilesA


    PathFileA = objFolderA.Path & "\" & objFileA.Name
    Debug.Print PathFileA
    PathFileB = objFolderB.Path & "\" & objFileA.Name
    Debug.Print PathFileB
    PathFileC = objFolderC.Path & "\" & objFileA.Name
    Debug.Print PathFileC
    
    If objFileA.Name Like "*.docx" Then
                    
        'Creating object of the word application
        Set WDApp = CreateObject("word.Application")
        
        'Making visible the word application
        WDApp.Visible = True
        
        'Opening the required word document
        Set WDDocA = WDApp.Documents.Open(PathFileA)
        

        'Opening the required word document
        Set WDDocB = WDApp.Documents.Open(PathFileB)
               
        WDApp.CompareDocuments _
        OriginalDocument:=WDDocA, _
        RevisedDocument:=WDDocB, _
        Destination:=wdCompareDestinationNew, _
        IgnoreAllComparisonWarnings:=False
        
        WDDocA.Close
        WDDocB.Close
        'On Error Resume Next
        'Kill objFolderC.Path & "\" & objFileA.Name
        'On Error GoTo 0
        
        'Turn off DisplayAlerts
        WDApp.DisplayAlerts = wdAlertsNone
       
        Set WDDocC = WDApp.ActiveDocument
        WDDocC.SaveAs FileName:=PathFileC
        WDDocC.Close SaveChanges:=True
    End If

        'Update of the progressbar and the label
        k = k + 1
        Me.LabelSummaryReport.Caption = k * 100 / filesNumber & "% Completed"
        Me.ProgressBarSummaryReport.Value = k * 100 / filesNumber
        
    Next objFileA
    Me.LabelSummaryReport.Caption = "The process is complete. Comparison reports have been created."
End Sub


Function ChooseFolder(title) As String
    Dim fldr As FileDialog
    Dim sItem As String

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .title = title
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With

NextCode:
    ChooseFolder = sItem
    Set fldr = Nothing
End Function

保存带有跟踪更改的摘要文档时遇到问题。无法保存此报告。我不知道具体怎么解决。

能否请您帮我解决此问题并在必要时优化此代码。

【问题讨论】:

  • 你在哪一行得到错误?在WDDocC.SaveAs FileName:=PathFileC 这个?你检查PathFileC了吗?
  • “评论”选项卡上有一个Compare 按钮和一个Track Changes 按钮。
  • 参见excelforum.com/word-programming-vba-macros/… 将两个文档与第三个文档作为输出文档进行比较,msofficeforums.com/133132-post2.html 将跟踪的修订导出到 Excel
  • pathFIleC 正确。事实上,当命令 'WDDocA.Close' 和 'DDDocB.Close' 完成时,所有 Word 文档都已关闭(摘要报告也)我得到了命令 'Set WDDocC = ActiveDocument'
  • @coeurdange57 在下面看到我的回答。您没有看到您的问题,因为您没有使用Option Explicit。如果您使用它并正确声明所有变量,您将收到通知,有些“变量”没有像 Excel 中的 wdAlertsNone 那样声明(它们只存在于 Word 中)。

标签: excel vba ms-word comparison


【解决方案1】:

确保您使用Option Explicit 来查看您的问题。

我建议始终激活Option Explicit:在 VBA 编辑器中转到 工具选项Require Variable Declaration。因此,您已在所有新代码中自动激活它。

如果您使用像 Set WDApp = CreateObject("word.Application") 这样的后期绑定,那么所有像 wdAlertsNonewdCompareDestinationNew 这样的 Word 枚举常量在 Excel 中都不存在。

所以要么你需要

  • 首先在 Excel 中定义它们
  • 或使用早期绑定(通过在 Extras > References 菜单中设置对 Word 的引用)
  • 或将所有 wd 常量替换为其特定的 Long 值。见Word Enumerated Constants

您还需要Set WDDocC = WDApp.ActiveDocument,因为Excel 期望ActiveDocument 是Excel 中的某个东西,但它并不存在,它只存在于Word 中。您需要指定您的意思是 Word 应用程序WDAppActiveDocument

【讨论】:

  • 抱歉,我不是 vba 专家。这里不知道如何解决保存文件的问题,因为当我关闭两个文件进行比较时,汇总报告也关闭了,所以系统无法保存...
  • @coeurdange57 你把它改成Set WDDocC = WDApp.ActiveDocument了吗?你做了什么?请更准确。进行我建议的更改在这里工作。
  • 请看我下面的帖子
  • @coeurdange57 请不要发布问题作为答案。如果您想在问题中添加一些内容,请编辑原始问题。答案应该只包含原始问题的解决方案(否则它们会被社区删除)。
  • 我用脚本的更新更新了我的原始帖子。我仍然有同样的问题。在保存文件的行中,出现错误“应用程序定义或对象定义错误”:Set WDDocC = WDApp.ActiveDocument WDDocC.Save As FileName:=PathFileC WDDocC.Close SaveChanges:=True
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 1970-01-01
相关资源
最近更新 更多