Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Apr 2003
    Location
    Connecticut, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sequential searchs in VBA (Word 97)

    Apologies if this has been answered before, but I didnt find anything in the archives that seemed applicable.

    I am trying to create a macro that will look through a document for certain text fragments and mark it for a table of contents. These text fragments are the screen and dialog names used in the application our company creates. I decided to use a search to find the beginning of the screen name, then use a second search to extend the selection to the end of the screen number, which immediately follows.

    Here is the relevant code:
    Dim rngname As String
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "^$"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .Font.Italic = True
    .Font.Bold = False
    .Style = "Table Text"
    Selection.Find.Execute
    While .Found = True
    Selection.Extend
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = ") "
    .Format = True
    .Font.Italic = False
    .Font.Bold = False
    .Style = "Table Text"
    .Replacement.Text = ""
    .Forward = True
    Selection.Find.Execute
    Selection.EscapeKey
    rngname = Selection.Text
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="TC rngname", PreserveFormatting:=False
    Selection.EndKey Unit:=wdLine
    End With
    .Execute
    Wend
    End With

    The problem is that the last .Execute statement goes to the second search, and I need it to go back to the first one. I obviously need some sort of flow-control to make this work. I tried moving the With...End WIth and Wend statements, bu that's a) not working and [img]/forums/images/smilies/cool.gif[/img] grasping at straws.

    How do you make sequential searches work?

    Thanks for any help.

    My office still uses Word 97 On WIndows NT, but we will be upgrading in the next quarter or two to 2000.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Sequential searchs in VBA (Word 97)

    This might be a good time to learn about wildcard searches. I think Word 97 had wildcard searches. If, as your code suggests, the text you're looking for always begins with certain text and ends with a closing parenthesis, you could search for something with a pattern like this:

    StartText*)

    Does that work? (It should be recordable...)

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Sequential searchs in VBA (Word 97)

    Regarding why the last execute seems to remember the later search settings instead of the earlier search settings, it is most likely because this is how Word works interactively. When you use the Selection object in VBA, you are working with the "real" selection and the behavior typically (but not always) emulates the interactive experience. Your code might work "as is" if you were to use ranges, but I really couldn't say for sure.

  4. #4
    New Lounger
    Join Date
    Apr 2003
    Location
    Connecticut, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sequential searchs in VBA (Word 97)

    jscher2000, thanks for the suggestion, but I don't think it's the search itself. I may have failed to explain the problem correctly.

    As you can see, I am using a wildcard ^$ to search for the beginning of hte text. I can get each individual search to work in isolation, and that works fine. In fact, the first time through the loop both searches find the required start and end of the required fragment. The next time through the loop, however, the second search is executed to find the end of the fragment, rather than the first search to find the start.

    Unless I'm not understanding your suggestion correctly.

  5. #5
    New Lounger
    Join Date
    Apr 2003
    Location
    Connecticut, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sequential searchs in VBA (Word 97)

    OOPS, simul-post.

    I tried to use the Selection.Find.ClearFormatting for each search to prevent that. If I added another such statement before the .Execute, what would happen?

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Sequential searchs in VBA (Word 97)

    I think if you put this before the .Execute you'll find that the problem is not the format, it's the find text:

    MsgBox "Find Text = " & .Text

    It is my belief that unlike many other kinds of object variables, changes to the properties you assign to the Selection object (and its Find sub-object, if that's a word, you know what I mean <img src=/S/wink.gif border=0 alt=wink width=15 height=15>) don't go away when you exit a With...End With or other kind of programming structure, because the Selection object is a "real" thing that represents window state and not a hypothetical construct like a range. Is this clearer than my last attempt?

  7. #7
    New Lounger
    Join Date
    Apr 2003
    Location
    Connecticut, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sequential searchs in VBA (Word 97)

    OK, I see what you mean now. So the the End WIth doesn't close off the Find properties within the loop. So all a .ClearFormatting after the End With will do is change the formatting of the text that is being searched for, not the search text. I suppose my options are to either a)use another Selection.Find to re-establish the "start text" or b)re-write it using ranges. I hate ranges, but I don't know if the While...Wend will still be true if I add another .Find

  8. #8
    New Lounger
    Join Date
    Apr 2003
    Location
    Connecticut, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sequential searchs in VBA (Word 97)

    Or, even better, use what you originally posted and select the text once.

Posting Permissions

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