2015-04-21 179 views
0

我已經使用Word完成了一個文本比較宏,我需要循環它以便它可以比較幾組文檔。有關如何這樣做的任何想法? 這裏是我的代碼循環比較宏

Dim template1 As Word.Document 
    Dim spool1 As Word.Document 
    Set template1 = Documents.Open("D:\Users\tmp4jj\Desktop\ComparisonTool\template1.docx") 
    Set spool1 = Documents.Open("D:\Users\tmp4jj\Desktop\ComparisonTool\spool1.txt") 
Application.CompareDocuments OriginalDocument:=Documents("template1.docx") _ 
    , RevisedDocument:=Documents("spool1.txt"), Destination:= _ 
    wdCompareDestinationNew, Granularity:=wdGranularityWordLevel, _ 
    CompareFormatting:=False, CompareCaseChanges:=True, CompareWhitespace:= _ 
    False, CompareTables:=True, CompareHeaders:=True, CompareFootnotes:=True, _ 
    CompareTextboxes:=True, CompareFields:=True, CompareComments:=True, _ 
    CompareMoves:=False, RevisedAuthor:="UOB", IgnoreAllComparisonWarnings:= _ 
    False 
ActiveWindow.ShowSourceDocuments = wdShowSourceDocumentsBoth 

,我也做了一個文件計數宏,不知道它需要

Dim FolderPath As String, path As String, count As Integer 
FolderPath = "D:\Users\tmp4jj\Desktop\ComparisonTool" 

path = FolderPath & "\*.docx" 
path = FolderPath & "\*.txt" 


FileName = Dir(path) 

Do While FileName <> "" 
    count = count + 1 
    FileName = Dir() 
Loop 

MsgBox count & " : files found in folder" 

Windows("Document1").Activate 
Application.Run MacroName:="FileCount" 
Application.Move Left:=-3, Top:=-3 
Application.Run MacroName:="FileCount" 
Application.Move Left:=-3, Top:=-3 
+0

注意,'路徑= FOLDERPATH& 「\ * DOCX。」'沒有任何影響 – moffeltje

回答

0

不完全知道你的意思,但你可以選擇一個批處理文件來使用操作的使用內置的文件對話框。

一個例子是:

Sub Example() 
    Dim item, templateDoc As Document, compareDoc As Document 

    'Get template file 
    With Application.FileDialog(msoFileDialogOpen) 
     .AllowMultiSelect = False 
     .Title = "Select template" 
     If .Show = 0 Then Exit Sub 
     Set compareDoc = Documents.Open(.SelectedItems(1)) 
    End With 

    'Get files to batch process 
    With Application.FileDialog(msoFileDialogFilePicker) 
     .AllowMultiSelect = True 
     .Title = "Select files to compare" 
     If .Show = 0 Then Exit Sub 
     For Each item In .SelectedItems 
      'Do some stuff 
      Set compareDoc = Documents.Open(item) 
      CompareDocs templateDoc, compareDoc 
      compareDoc.Close 
     Next 
    End With 

End Sub 
Sub CompareDocs(templateDoc As Document, compareDoc As Document) 
    Application.CompareDocuments _ 
      OriginalDocument:=templateDoc, _ 
      RevisedDocument:=compareDoc, _ 
      Destination:=wdCompareDestinationNew, _ 
      Granularity:=wdGranularityWordLevel, _ 
      CompareFormatting:=False, _ 
      CompareCaseChanges:=True, _ 
      CompareWhitespace:=False, _ 
      CompareTables:=True, _ 
      CompareHeaders:=True, _ 
      CompareFootnotes:=True, _ 
      CompareTextboxes:=True, _ 
      CompareFields:=True, _ 
      CompareComments:=True, _ 
      CompareMoves:=False, _ 
      RevisedAuthor:="UOB", _ 
      IgnoreAllComparisonWarnings:=False 
    ActiveWindow.ShowSourceDocuments = wdShowSourceDocumentsBoth 
End Sub