View Single Post
 
Old 10-01-2019, 05:48 AM
scienceguy scienceguy is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default Batch Comparing Multiple Word Files

Hello,

I am trying to write an application, whereby Word vba will compare Word files in one folder (original version) with the corresponding files in another folder (updated or revised version) and show the track changes. The application would process one file at a time and save the tracked changed file as "original_file_name_tracked.docx". In the end, the application would have generated a tracked change version of each set of files (original versus revised).

To accomplish this, I recorded a macro in Word vba that processed the first file. I then modified the code to work on an entire folder, one file at a time. Unfortunately, I am getting a run-time error 4160 (Bad File Name). I went back to the original recorded macro and ran it (without any modifications on my part), and it also throws the same error. If any one has any suggestions, I would appreciate it. Below is my code for processing a folder.

Thank you in advance for any help!

Sincerely,
Roy


Code:
Sub trackMe()
'
' trackMe Macro
'
Dim strFolderOriginal As String, strFolderUpdated As String
Dim strfile As String
Dim tempFileName As String

'original
With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = -1 Then
        strFolderOriginal = .SelectedItems(1)
    End If
End With
If strFolderOriginal = "" Then
    Exit Sub
End If

'updated
With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = -1 Then
        strFolderUpdated = .SelectedItems(1)
    End If
End With
If strFolderUpdated = "" Then
    Exit Sub
End If

strfile = Dir(strFolderOriginal & "\*.doc", vbNormal)

While strfile <> ""

    tempFileName = stripFileName(strfile) 'remove file extension
    
    ChangeFileOpenDirectory strFolderOriginal & "\"
    ChangeFileOpenDirectory strFolderUpdated & "\"
    
    Application.CompareDocuments OriginalDocument:=Documents( _
        strfile) _
        , RevisedDocument:=Documents( _
        strfile) _
        , Destination:=wdCompareDestinationOriginal, Granularity:= _
        wdGranularityCharLevel, CompareFormatting:=True, CompareCaseChanges:=True _
        , CompareWhitespace:=True, CompareTables:=True, CompareHeaders:=True, _
        CompareFootnotes:=True, CompareTextboxes:=True, CompareFields:=True, _
        CompareComments:=True, CompareMoves:=True, RevisedAuthor:="Author", _
        IgnoreAllComparisonWarnings:=False
        
    ChangeFileOpenDirectory strFolderOriginal & "\"
    
    ActiveDocument.SaveAs2 FileName:= _
        tempFileName & "_tracked.docx" _
        , FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False, CompatibilityMode:=12
        
    strfile = Dir()
Wend

MsgBox "Finished"

End Sub
Function stripFileName(strFilename As String) As String
'used to remove file extension

Dim k As Integer

For k = Len(strFilename) To 1 Step -1
    If Mid(strFilename, k, 1) = "." Then
        stripFileName = Mid(strFilename, 1, k - 1)
        Exit For
    End If
Next k


End Function
Reply With Quote