Consulting

Results 1 to 9 of 9

Thread: Loop to find and replace in a Word document

  1. #1

    Loop to find and replace in a Word document

    Hi All,

    I'm new to VBA and havent got a clue where to start on this. I need help on a macro that i can run that will find all instances of a string and replace it with only the middle part of the string e.g.

    htps:/abc.kr/p[img]htps:/abc123.jpg[/img] fbcde5678 mr smith[/url]

    to be replace with just [img]htps:/abc123.jpg[/img]

    htps:/abc.kr/p[img]htps:/dce456.jpg[/img] kabcd1234 mr smith[/url]

    replace with [img]htps:/dce456.jpg[/img]
    ...etc...

    i.e. all strings will start with "htps:/abc.kr/p" and end with "mr smith[/url]"

    I want to replace all these and only want to keep the section between the [img] and [/img] including those tags.

    Any help appreciated. Thanks.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,242
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "(htps:/abc.kr/p)(*)( *mr smith\[/url\])"
        .Replacement.Text = "\2"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Superb Greg - thank you very much!

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,174
    Location
    Do be aware that you don't actually need a macro for this. You can do the same with a wildcard Find/Replace (which is all the macro uses), where:
    Find = (htps:/abc.kr/p)(*)( *mr smith\[/url\])
    Replace = \2
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Thanks Paul, very useful, i couldnt find any help online for Find and Replace that reflected that. Cheers

  6. #6
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    7,478
    Location
    MS tends to hide all the really powerful stuff like Paul's suggestion


    In the [Replace] dialog, click [More>>] and [Wildcards] which makes different rules apply. Word uses something close to Regular Expressions


    [Special] opens another list of Word-peculiar symbols, including the RegEx grouping \n one. Some work in Find and some only work in Replace (MS's way to keep you on your toes)

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,174
    Location
    Quote Originally Posted by finn_2018 View Post
    i couldnt find any help online for Find and Replace that reflected that
    See:
    https://support.office.com/en-us/art...4-7708c6c779b7
    http://wordmvp.com/FAQs/General/UsingWildcards.htm
    http://www.gmayor.com/replace_using_wildcards.htm
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8

    Can I ask for a similar solution here?

    Quote Originally Posted by gmaxey View Post
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "(htps:/abc.kr/p)(*)( *mr smith\[/url\])"
        .Replacement.Text = "\2"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    I have a word document which has drop down lists inserted in some parts of tables and main texts inside the document:





    I need to be able to search inside the document , including the drop down list options (whether the dropdown item is selected or not), for the phrase "INSERT" and "Select", and when either is found, replace them numerically with "Field 1" , "Field 2". For the numbering, 1 is the first found instance at the top of the document.
    My main current issue is no advanced or basic "Find" function includes the dropdown lists in word, how can I search them? I initially tried to do lists separately, and got this far:


    Sub LoadSchedule()
        Dim objCC As Integer 
        Dim objCL As Integer 
         
        For objCC = 1 To ActiveDocument.ContentControls.Count
         If ActiveDocument.ContentControls(objCC).Type = wdContentControlComboBox Or _
         ActiveDocument.ContentControls(objCC).Type = wdContentControlDropdownList Then
      
            For objCL = 1 To objCC.DropdownListEntries.Count
                ActiveDocument.ContentControls(objCC).DropdownListEntries(objCL).Select
              
                Selection.Find.ClearFormatting
                Selection.Find.Replacement.ClearFormatting
    
                'Here do the search inside selected area and name each found
                'area iterativley
            Next
         End If
        Next
    End Sub

    However Ideally the whole thing would be done in one loop, any and all help is appreciated.

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,174
    Location
    Instead of resurrecting old threads just to hijack them with something quiet different to what the original thread is about, kindly start a new thread of your own, cross-referencing this one if appropriate.

    Moreover, don't post the same question multiple times as you have done in this case.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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