I have a macro that validates an Active Document word by word. If a word in Active Document is listed in a "Bible of Terms", then the macro adds a comment to the Active Document with a suggestion to replace that word with a better one. Example: Term= "He is ", Suggestion= "He's ". The Bible contains a table with 600 terms, one term per row. Column 1=term, Column 2= suggestion). My code loops through 600 terms in the Bible. This is inefficient because if the document I'm validating only has one word, the loop still runs 600 times. How can I stop the code when the document I'm validating reaches its end? Or don't even run the process if the document has nothing.
Dim BibleCounter as Integer
Dim MaxWordsInBible as Integer
MaxWordsToValidate = (ActiveDocument.Words.Count - 1) 'Count words in doc to validate
MaxWordsInBible = Documents(MatrixDocNum).Tables(1).Rows.Count 'Count rows in bible
For BibleCounter = 2 To MaxWordsInBible 'Currently loops 600 times
Set findRange = ActiveDocument.range 'Activedocument I'm validating against Bible
With findRange.Find
.Text = BibleFile.Tables(1).Rows(BibleCounter).Cells(1).range.Text
.MatchWholeWord = False
.Wrap = wdFindStop 'stops find at the end of the document
'Loop to find the suggestion to replace a word in the activedocument with a term in the Bible
Do While .Execute(Forward:=True) = True
suggestion = BibleFile.Tables(1).Rows(BibleCounter).Cells(2).range.Text
ActiveDocument.Comments.Add findRange, Text:=suggestion
findRange.Collapse wdCollapseEnd 'to avoid endless loop
Loop 'do while
End With 'findRange.Find
Next BibleCounter
CodePudding user response:
I am sure that there are multiple solutions. My solution is to use inrange (pesudocode);
dim searchRange as Range = activeDocument.content
Dim findRange as range = searchRange.duplicate
findRange.find.execute
Do
If findRange.find.found andalso findRange.inrange(searchRange) then
‘ do processing
End if
findrange.collapse (end)
findRange.find.execute
While findrange.find.found
CodePudding user response:
You could expedite things a bit by disabling ScreenUpdating. The code could also be improved somewhat, especially regarding how you retrieve the table text:
Application.ScreenUpdating = False
Dim BibleFile As Document, Tbl As Table
Set BibleFile = "some document"
Set Tbl = BibleFile.Tables(1)
For BibleCounter = 2 To Tbl.Rows.Count 'Currently loops 600 times
With ActiveDocument.Range 'Activedocument I'm validating against Bible
With .Find
.Text = Split(Tbl.Cell(BibleCounter, 1).Range.Text, vbCr)(0)
.MatchWholeWord = False
.Forward = True
.Wrap = wdFindStop 'stops find at the end of the document
'Loop to find the suggestion to replace a word in the activedocument with a term in the Bible
.Execute
End With
Do While .Find.Found = True
.Comments.Add Range:=.Duplicate, Text:=Split(Tbl.Cell(BibleCounter, 2).Range.Text, vbCr)(0)
.Collapse wdCollapseEnd 'to avoid endless loop
.Find.Execute
Loop
End With
Next
Application.ScreenUpdating = True
Looping through the whole table isn't likely to add a lot of time for expressions that aren't found - it's processing all the found ones that is likely to consume most of the time.
Of course, if the document has only a few words, you should consider reversing the process and using that document as the source to check against your BibleFile.