erwarren
08-12-2015, 02:40 PM
Long time reader, first time poster here. I can't seem to figure this one out, so any help would be greatly appreciated.
Background:
I have a macro that produces a list of all the paragraph styles in use in this format:
Normal -- p. 1
Heading 1 -- p. 12
No Spacing -- p. 144
Each style in use is only listed once, and the page numbers, of course, will vary based on the document.
If the only styles in use are "Endnote Text" and/or "Footnote Text" something went wrong so I need to check if the list consists of ONLY combinations of those. My plan is to paste the string that contains the list in a new document so I can use Selection.Find and wildcards to search the text, and then check if the length of the Selection.Text is the same as the length of the whole list. Ideally I'd have only 1 (possibly 2) patterns that could find all combinations of those two styles.
Actual Problem:
I'm starting by trying to find a wildcard search that will find either "Endnote Text -- p. 000" or "Footnote Text -- p. 000" (where 000 is an unknown page number), and then I figure I can double it up to search for both. I've come up with this, entered as an image because my post got rejected with this string in it:
14139
But it only finds "Footnote Text -- p. 000", not "Endnote Text -- p. 000". I think the issue is the {3,4} part, which I took to mean "three or four instances of the previous expression". If I change it to just {3} I match "Endnote Text -- p. 000" as expected as well as the "ootnote Text -- p. 000" of the other style, but once I add the range it stops matching "Endnote Text -- p. 000".
That's it in a nutshell, but here's the code I've written for the whole sub so far.
Sub test()
' This will ultimately be a boolean function and strSearchPattern
' and strSearchText will be passed to it when called
Dim strSearchPattern As String
Dim strSearchText As String
' Here would be the same string in the image above
strSearchText = "Endnote Text -- p. 100"
Documents.Add Visible:=False
Selection.InsertAfter (strSearchText)
With Selection.Find
.ClearFormatting
.Text = strSearchPattern
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWholeWord = False
.MatchCase = True
.MatchWildcards = True
.MatchSoundsLike = False
.Execute
End With
If Selection.Find.Found = True Then
If Len(Selection.Text) = Len(strSearchText) Then
Debug.Print "True"
Else
Debug.Print "False"
End If
End If
ActiveDocument.Close wdDoNotSaveChanges
End Sub
Also FWIW, I'm using Word 2013 on Windows 8 but this needs to work for all versions 2007-2013, including Mac 2011.
Background:
I have a macro that produces a list of all the paragraph styles in use in this format:
Normal -- p. 1
Heading 1 -- p. 12
No Spacing -- p. 144
Each style in use is only listed once, and the page numbers, of course, will vary based on the document.
If the only styles in use are "Endnote Text" and/or "Footnote Text" something went wrong so I need to check if the list consists of ONLY combinations of those. My plan is to paste the string that contains the list in a new document so I can use Selection.Find and wildcards to search the text, and then check if the length of the Selection.Text is the same as the length of the whole list. Ideally I'd have only 1 (possibly 2) patterns that could find all combinations of those two styles.
Actual Problem:
I'm starting by trying to find a wildcard search that will find either "Endnote Text -- p. 000" or "Footnote Text -- p. 000" (where 000 is an unknown page number), and then I figure I can double it up to search for both. I've come up with this, entered as an image because my post got rejected with this string in it:
14139
But it only finds "Footnote Text -- p. 000", not "Endnote Text -- p. 000". I think the issue is the {3,4} part, which I took to mean "three or four instances of the previous expression". If I change it to just {3} I match "Endnote Text -- p. 000" as expected as well as the "ootnote Text -- p. 000" of the other style, but once I add the range it stops matching "Endnote Text -- p. 000".
That's it in a nutshell, but here's the code I've written for the whole sub so far.
Sub test()
' This will ultimately be a boolean function and strSearchPattern
' and strSearchText will be passed to it when called
Dim strSearchPattern As String
Dim strSearchText As String
' Here would be the same string in the image above
strSearchText = "Endnote Text -- p. 100"
Documents.Add Visible:=False
Selection.InsertAfter (strSearchText)
With Selection.Find
.ClearFormatting
.Text = strSearchPattern
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWholeWord = False
.MatchCase = True
.MatchWildcards = True
.MatchSoundsLike = False
.Execute
End With
If Selection.Find.Found = True Then
If Len(Selection.Text) = Len(strSearchText) Then
Debug.Print "True"
Else
Debug.Print "False"
End If
End If
ActiveDocument.Close wdDoNotSaveChanges
End Sub
Also FWIW, I'm using Word 2013 on Windows 8 but this needs to work for all versions 2007-2013, including Mac 2011.