PDA

View Full Version : Finding a Specific List String



heedaf
08-28-2017, 11:10 AM
Is there a way to have word find a specific list string? All I have been able to do is loop through the headings until the correct numbering is found. Something like the following is what I would have thought could work (".ListString = "1.1").

Sub Sample()
Dim c As Range
Dim StartWord As String, EndWord As String
StartWord = "Start": EndWord = "End"
Set c = ActiveDocument.Content
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting
With c.Find
.Text = StartWord & "*" & EndWord
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False '

.ListString = "1.1"

End With
c.Find.Execute

End Sub

macropod
08-28-2017, 04:37 PM
You need to learn which properties work with what objects - which you can do by looking them up in the VBE. You can't just slap them together without regard for that.
Since you're looking for heading content, try something along the lines of:

Sub Sample()
Const StartWord As String = "Start"
Const EndWord As String = "End"
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = StartWord & "*" & EndWord
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.Style = "Heading 2"
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Paragraphs(1).Range.ListFormat.ListString = "1.1" Then
'Do something
Exit Do
End If
.Collapse
.Find.Execute
Loop
End With
End Sub
Note the use of:
.Style = "Heading 2"
This tells Word to ignore anything that's not in the 'Heading 2' Style - the Style most likely to have a 1.1 liststring. You can, of course, change the Style name to something else or delete the reference altogether (in which case you'd want to revert to .Format = False).

heedaf
08-28-2017, 07:20 PM
Thank you for the reply. I just added .liststring = " " to give an idea on what I was thinking. The solution you gave is how I'm currently doing the search, I was just hoping for a faster way of searching since I have a very large document I'm working with.

macropod
08-28-2017, 07:23 PM
I just added .liststring = " " to give an idea on what I was thinking. The solution you gave is how I'm currently doing the search
For future reference, kindly post the code you're actually using. I'd rather not waste my time reinventing the wheel you're already using...

heedaf
08-28-2017, 07:54 PM
Sorry! I thought I put that I was looping through the code already.

macropod
08-28-2017, 11:26 PM
What you actually said was:

All I have been able to do is loop through the headings until the correct numbering is found.
There's nothing about that to suggest whatever 'looping' code you referred to used Find/Replace and the Find/Replace code you posted contained not a hint of a loop.

Regardless, the point is that the code you posted wasn't what you were already using and led to me wasting time reinventing your wheel.