【发布时间】: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