dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Determine whole word after finding first letter

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,028

    Resolved [RESOLVED] Determine whole word after finding first letter

    Given code like this which looks for headings of a certain style that start with "M", how do I Debug.Print the word in the heading that contains the "M"? It will always be the first word. Are the .Text... and .Execute... lines correct?
    Code:
    Set rng = ActiveDocument.Content
    
    Do
        With rng.Find
            .Text = "M*"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Style = ActiveDocument.Styles("Heading 3,Sub-clause Heading,1R Contents Heading 3,VS3")
            .ClearFormatting
            .Execute FindText:="M"
    
            Debug.Print <Whole first word of heading that's found>
    
            ' more code

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,886

    Re: Determine whole word after finding first letter

    wouldn't you need matchwildcards?

    on testing, this worked
    Code:
        With rng.Find
            .Text = "S*"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Style = ActiveDocument.Styles("Heading 3") ', "Sub-clause Heading,1R Contents Heading 3,VS3")
            .ClearFormatting
            .Execute
        End With
            Debug.Print rng.words(1) '<Whole first word of heading that's found>
    i am not sure how or if you can specify multiple styles, possibly using the styles constants, the findtext parameter would override the .text
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width