+ Reply to Thread
Results 1 to 33 of 33

Find/Replace Macro to Word

Hybrid View

  1. #1
    Registered User
    Join Date
    09-28-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Find/Replace Macro to Word

    Ok thanks again, but this is much different to what I posted and for a macro beginner makes no sense at all;

    Who is Tom, Ken, Harry, ******

    a(1, 1) = "****"
    a(2, 1) = 2
    a(1, 2) = "Harry"
    a(2, 2) = 3
    For i = 1 To 2

    This i don't understand either.

    I wanted the code I posted to be adjusted to support multiple find/replaces as a long loop;

    Sub Replacing()
    
        Dim sFile   As String
        Dim wrdApp  As Word.Application
        Dim wrdDoc  As Word.Document
    
        sFile = "Pack"
        Set wrdApp = New Word.Application
    
        With wrdApp
            .Visible = True
            Set wrdDoc = .Documents.Open(ThisWorkbook.Path + "\" + sFile + ".doc")
        End With
    
        With wrdDoc.Content.Find
            .Text = "NAME1"
            .Replacement.Text = Range("C2").Text
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
            .ClearFormatting
            .Replacement.ClearFormatting
        End With
    
     With wrdDoc.Content.Find
            .Text = "NAME1"
            .Replacement.Text = Range("C2").Text
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
            .ClearFormatting
            .Replacement.ClearFormatting
    
     With wrdDoc.Content.Find
            .Text = "NAME2"
            .Replacement.Text = Range("C3").Text
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
            .ClearFormatting
            .Replacement.ClearFormatting
    
     With wrdDoc.Content.Find
            .Text = "Payment One"
            .Replacement.Text = Range("G5").Text
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
            .ClearFormatting
            .Replacement.ClearFormatting
    
    'And so on until it has 50 of these.
        
    wrdDoc.PrintOut
    wrdDoc.Close False
    
    wrdApp.Quit
    
    End Sub
    I understand what your trying to do, your new code is probably much better for me but I cannot understand it one but nor attempt to amend it.

    If you can just adjust the code in this post I would be grateful.

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Find/Replace Macro to Word

    I have given you 3 solutions. I don't know how many solutions you want. This last Test sub showed how to send one Find/Replace at a time. It also showed how to setup an array so that it could do many in a "loop". In the array a(), the first dimension 1 or the x in a(x,y). It is the Find string. Think of it as a column. The second dimension of a() is the Replace string y in the array, a(x, y). Think of it as a row though some think opposite but arrays don't care. This is the same as if we set a range of 2 columns and 2 rows.

    The first solution that I gave is the more robust. It took care of the merged cell issue. You will have to do that yourself if you use most any other method.

    The forum changed the work DDIICCKK (less duplicated letters) to "****".
    The search replace was:

    Search Replace
    Tom 1

    By array a() For Loop:
    DDIICCKK 2
    Harry 3

    Ken 4.

    So you see: Tom, DDIICCKK, Harry, Ken; are the search strings. The replace strings were: 1, 2, 3, 4. The first dimension x in a(x, y) will be either 1 for the search string or 2 for the replace string.

    Notice that by the parameters sent to the sub, the first call did one search/replace group. The array method looped and did 2 calls to the sub. In all 3 calls, the parameters were set to not close the MSWord application nor the DOC.

    For the last call, it did close MSWord, if it was not opened, and then closed the DOC. Of course when it does, it will ask you to save it. You can record a macro to do the save and print as needed just after "If closeDoc".
    Last edited by Kenneth Hobson; 09-29-2011 at 04:44 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1