Consulting

Results 1 to 6 of 6

Thread: Finding a Specific List String

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location

    Finding a Specific List String

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by heedaf View Post
    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...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    Sorry! I thought I put that I was looping through the code already.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    What you actually said was:
    Quote Originally Posted by heedaf View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •