Hi Sam,
Paul is correct in pointing out your request for code on the Microsoft forums. You could have provided a cross reference to the thread with possible solutions that others could have referenced and used for further development. I have been searching and playing around with code to try assist (and this page could have assisted me), partly as I had some time, but also as I felt challenged to try; bearing in mind my Word VBA is weak. Anyways, please ensure you do refer to any cross-posting you have done on other sites. It is considered good manners.
I cannot say the code below will work or not, but I tried to merge your code with code
from here (by Frosty), which tries to completely skip tables altogether instead of ignoring tables but still scanning through them. You will need to see first if the code works and second if it speeds things up by actually skipping the tables completely. It looks lengthy because the code needs to be doubled up to run one more time for all data below the last table.
Try this:
Code: Select all
Public Sub AnotherWayToDoHorribleThingsToACat()
Dim oTable As Table
Dim rngWorking As Range
Dim lStart As Long
Const message As String = "Widow/Orphan = False"
Dim oPar As Paragraph
Dim oRng As Word.Range
'get our starting position
lStart = ActiveDocument.Content.Start
'intialize our range
Set rngWorking = ActiveDocument.Content
'get the range of everything in front of the current table
For Each oTable In ActiveDocument.Tables
rngWorking.Start = lStart
rngWorking.End = oTable.Range.Start
With rngWorking
With .Find
.ClearFormatting
.Text = "^13"
.Execute
End With
If .Paragraphs.WidowControl = False Then
If .Find.Found Then
.Select
Selection.Comments.Add Range:=Selection.Range
Selection.TypeText Text:=message
Set oRng = Nothing
End If
End If
End With
lStart = oTable.Range.End
Next
'now that we're done, we need everything after the last table to the end of the document
rngWorking.Start = ActiveDocument.Tables(ActiveDocument.Tables.Count).Range.End
rngWorking.End = ActiveDocument.Content.End
With rngWorking
With .Find
.ClearFormatting
.Text = "^13"
.Execute
End With
If .Paragraphs.WidowControl = False Then
If .Find.Found Then
.Select
Selection.Comments.Add Range:=Selection.Range
Selection.TypeText Text:=message
Set oRng = Nothing
End If
End If
End With
End Sub