If your viewing this then either your looking for the same help as me, or your here to help. Both are welcome!
So Im trying to create a macro that I can use to search the contents of a document, locate numbers with a checksum, check if the checksum is correct, then highlight or format the text to indicate correct/incorrect.
For those that are new to the concept of checksums I'll explain it. Please bear in mind I dont mean for this to sound condecending at all. If you know how checksums work you can skip this example.
Example: We'll use a time in Zulu.
170006Z4 FEB 18
That shows the 17th day of Feb 2018. The checksum "4" after the "Z" (indicating the time zone) is calculated by adding all the numbers up and using the number that shows in the ones place.
1 + 7 + 0 + 0 + 0 + 6 = 14
1 (ten's place) 4 (one's place)
We drop everything but the one's place and add it after the time zone.
This is the code I'm working with but I'm not really familar with the .find function and the documentation is a bit confusing on it. This only highlights the first item green and the rest of them red from the example below. I can't figure out why. This is only one type of number I'm looking to check.
The output I'm getting:
122323z3
122323z1
122323z8
010000Z1
122323z2
What I expected:
122323z3
122323z1
122323z8
010000Z1
122323z2
END PRODUCT: What i'm trying to do is check a list of formats and verify the checksums, but I dont mind just using copy and paste and just change the target text and run more then one operation in a row. In the end I'll be checking for these formatsSub Verify_Checksums() Dim TargetItem As String Dim CheckSum As Long Dim NumberSum As Long Dim Value As String Dim i As Long With Selection .HomeKey wdStory With .Find .ClearFormatting .Text = "[0-9]{6}[A-Za-z]{1}[0-9]{1}" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False 'MUST USE WILDCARDS OR IT DOESN'T WORK .MatchWildcards = True End With While .Find.Execute 'Verify checksum Value = .Text TargetItem = .Text CheckSum = Right(TargetItem, 1) For i = 0 To Len(TargetItem) - 3 NumberSum = NumberSum + Left(TargetItem, 1) TargetItem = Right(TargetItem, Len(TargetItem) - 1) Next i NumberSum = Right(NumberSum, 1) If NumberSum = CheckSum Then 'MATH EQUALS THE CHECKSUM LET HIGHLIGHT IT GREEN .Text = Value .Font.Color = wdColorGreen Else 'MUST BE WRONG SO LETS HIGHLIGHT IT RED .Text = Value .Font.Color = wdColorRed End If Wend End With End Sub
The "0" indicates numbers, "X" indicates letters, and "/" indicates the symbol "/"
0/0
00/0
00000/0
0X0
00X0
000X0
0000X0
00000X0
000000X0 <----- This is the most common format so I'm using this for my testing.
0000000X0
Any help with this would be wonderful. I'm going to keep plugging away trying to figure this out.
Thanks!