Need help with this Macro

aiwnjoo

Well-known Member
Joined
Jul 30, 2009
Messages
598
Code:
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("C:\Users\Admin\Desktop\" + sFile + ".doc")

        With .Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "NAME1" + "NAME2"
            .Replacement.Text = Range("C2") + Range("C3")
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
        End With
    End With
End Sub

.Text = "NAME1"
.Replacement.Text = Range("C2")

This works for all NAME1 entries in the Word document but I want to have multiple searches for example;

.Text = "NAME1" & "NAME2"
.Replacement.Text = Range("C2") & Range("C3")

So that it will replace NAME1 and 2 with what is in C2 and C3. I want to do about a hundred of these.

Also I would like some assistance on how to modify this macro so the user cannot interact with it and once the changes are complete it Prints and Closes the word document.

Many thanks.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Ok, snb gave me a solution but im confused how to amend and it does not work;

Code:
Sub Replacing()
    With ThisWorkbook.Sheets(1)

    c01 = .Range("C2") & .Range("C3")
    
    End With

With GetObject("C:\Users\Admin\Desktop\Pack.doc")
    .Selection.Find.Execute "NAME1", "NAME2", , , , , , c01, 2
    .Close -1
 
    End With

End Sub

Any advice?
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top