Reputation: 1
Is there a Word VBA code that can compare the digits in two Word documents, leaving all non-digit text uncompared? The first document is always in English and the second document is its translation (Arabic, Spanish, etc.).
The purpose of the comparison is to make sure that all digits (numerals) in English and in the target language match.
The normal "Compare" tool compares everything (both digits and text), and the resulting compare document would be a mess).
I have highlighted all digits in both documents (using "([0-9])" as a Wildcard), extracted all highlights into two new Word documents using a VBA code, and then compared the two resulting files, but the compare was meaningless. That is why I hope a compare can be done on digits only (leaving non-digit text intact).
Upvotes: 0
Views: 128
Reputation: 5075
The following may give you a starting point for your VBA code.
Sub Main()
Dim Match As Object
Dim Matches As Object
Dim match_1 As String: match_1 = ""
Dim match_2 As String: match_2 = ""
' the documents you are comparing
Dim page_1 As Document: Set page_1 = Word.Documents(1)
Dim page_2 As Document: Set page_2 = Word.Documents(2)
Dim Expression As Object: Set Expression = CreateObject("vbscript.regexp")
' expression to match positive, negative and unsigned numbers with or without a decimal place
Expression.Pattern = "[+ -]?[0-9]+([.][0-9]+)?"
Expression.Global = True
' populate match_1 with the numbers matched in the page_1
Set Matches = Expression.Execute(page_1.Content.Text)
For Each Match In Matches
If match_1 = "" Then
match_1 = Trim$(Match.Value)
Else
match_1 = match_1 & ", " & Trim$(Match.Value)
End If
Next Match
' the same for page 2
Set Matches = Expression.Execute(page_2.Content.Text)
For Each Match In Matches
If match_2 = "" Then
match_2 = Trim$(Match.Value)
Else
match_2 = match_2 & ", " & Trim$(Match.Value)
End If
Next Match
' simple compare
If match_1 = match_2 Then
Debug.Print "Match"
Else
Debug.Print "Mismatch"
End If
End Sub
Upvotes: 0