Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Oct 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search and replace inWord (vba Word 2000)

    Is someone able to help-me with the following code snippets? In each case I wish to be able to search for text in a Word document and modify the formatting of that text and or some surround text

    1. search for

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and replace inWord (vba Word 2000)

    Here is a macro for #1:

    Sub Replace1()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "abc"
    .MatchWholeWord = False
    .MatchWildcards = False
    .Wrap = wdFindStop
    Do While .Execute
    Selection.Font.Hidden = True
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Font.Color = wdColorRed
    Selection.Collapse Direction:=wdCollapseEnd
    Loop
    End With
    End Sub

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and replace inWord (vba Word 2000)

    And here is a macro for #2:

    Sub Replace2()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "def"
    .MatchWholeWord = False
    .MatchWildcards = False
    .Wrap = wdFindStop
    Do While .Execute
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Font.Hidden = True
    Selection.Collapse Direction:=wdCollapseEnd
    Loop
    End With
    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and replace inWord (vba Word 2000)

    And #3:

    Sub Replace3()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "hij"
    .MatchWholeWord = False
    .MatchWildcards = False
    .Wrap = wdFindStop
    Do While .Execute
    Selection.Text = "klm"
    Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph
    Selection.TypeText "hij"
    ActiveDocument.FormFields.Add Selection.Range, wdFieldFormTextInput
    Loop
    End With
    End Sub

    Note: you can insert Application.ScreenUpdating = False at the beginning of each of the macros, and Application.ScreenUpdating = True at the end. This will speed up execution.

  5. #5
    New Lounger
    Join Date
    Oct 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and replace inWord (vba Word 2000)

    Thanks heaps for your help, your code does the job marvellously. Would you also mind helping me with the following:

    4. If my text is:
    ab a variety of text here
    that may be on multiple lines
    ab this would be different text
    also on multiple lines
    zx

    Can you help me with script that would search for

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and replace inWord (vba Word 2000)

    #4 can be done without code: in the replace dialog, tick the Use wildcards box. Enter ab*ab in the Find what box, and ^& in the Replace with box (this stands for the text in the Find what box). With the insertion point in the Replace with box, click Formatting and set the desired formatting. Then, click Replace all. In code:

    Sub Replace4()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .Text = "ab*ab"
    .Replacement.Text = "^&"
    .Replacement.Font.Color = vbRed
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With
    End Sub

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and replace inWord (vba Word 2000)

    #5: Performing a separate pass through the document for each search string is probably more efficient, for if you want to do it in one pass, you would probably have to move through the text of the document and compare it to each of the search strings; this will be much slower than using the built-in Find object.

  8. #8
    New Lounger
    Join Date
    Oct 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and replace inWord (vba Word 2000)

    Hans, thanks again for your help with this. I am having trouble with the last code you suggested. It works really well where I want to search & replace mincludes the 2i dentifiers eg ab . . . .ab as below:

    ab
    xxxx
    xxxx
    ab

    however, the case I need to be able to handle starts with the ab and stops before the second eg: i.e. I need to be able to search for ab and all the following text until the last character before the next , then I need to be able to repeat the same for any further occurrences in the document.

    Also, the character is treated in a special way by vba. How do I search for it?

    Thanks

    Walbert

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and replace inWord (vba Word 2000)

    If you want to do find the text from an occurrence of ab up to but not including the next occurrence, you will end up finding almost the entire document, since the next search action will start where you left off. I assume that is not what you really want. Perhaps this?

    Sub Replace4a()
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "ab*ab"
    .MatchWholeWord = False
    .MatchWildcards = True
    .Wrap = wdFindStop
    Do While .Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
    Selection.Font.Color = vbBlue
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.MoveRight Unit:=wdCharacter, Count:=3
    Loop
    End With
    End Sub

    If you want to search for a single backslash with wildcards "on", search for a double backslash .

  10. #10
    New Lounger
    Join Date
    Oct 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and replace inWord (vba Word 2000)

    Thanks Hans, that is what I wanted. Thanks again for all your help. It has been of enormous help to me

    Walbert

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and replace inWord (vba Word 2000)

    Just out of curiosity, what are you using all this code for? (You don't have to answer if you don't feel like it)

  12. #12
    New Lounger
    Join Date
    Oct 2003
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and replace inWord (vba Word 2000)

    We use a specialist app in our office which stores its data in a text file with each record identified and separated by backslash markers. ie abc We have a need to have this data updated by people with very modest computer skills - rural villiage people in Asia, and we do not want to have to teach them the specalist app.

    So I am taking the text file and via the macro, making some changes to it and adding in text fields where I want them to do the data entry. Then I protect the form and give them the Word file and they fill in the blanks using Word.

    When the completed file(s) (some are a couple of hundred pages long) are returned to us, we save them as text and so lose all the Word formatting and end up with a text file that we can open in the special app without any other conversion required.
    The maco is great cause it allows us to recreate the forms whenever we update any of our various sets of base data.

    Thanks again.

    Walbert

Posting Permissions

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