Two Macros for Find/Replace - Which is better?

aiwnjoo

Well-known Member
Joined
Jul 30, 2009
Messages
598
Apologies for all me recent bumps, I have now found two solution that slightly differ from eachother and they both work.

I would like your views on each one, any bugs or performance issues so I know I am using the correct one.

Summary: The macro will use cells from the excel sheet and find/replace the match in a word letter to auto generate a pre-filled letter then print and close.

Macro 1:

Code:
Sub Replacing()

    Dim sFile     As String
    Dim wrdApp    As Word.Application
    Dim wrdDoc    As Word.Document
    Dim sInput(7) As String, sOutput(7) As String

    sFile = "Pack"
    Set wrdApp = New Word.Application

    With wrdApp
        .Visible = True
        Set wrdDoc = .Documents.Open("C:\Users\Admin\Desktop\" + sFile + ".doc")
    
    .Selection.Find.ClearFormatting
    .Selection.Find.Replacement.ClearFormatting
    
    sInput(0) = "C2"
    sInput(1) = "C3"
    sInput(2) = "C8"
    sInput(3) = "C9"
    sInput(4) = "C10"
    sInput(5) = "C11"
    sInput(6) = "C12"
    
    sOutput(0) = "NAME1"
    sOutput(1) = "NAME2"
    sOutput(2) = "Address Line 1"
    sOutput(3) = "Address Line 2"
    sOutput(4) = "Address Line 3"
    sOutput(5) = "Address Line 4"
    sOutput(6) = "Address Line 5"
    
    For i = 0 To UBound(sInput) - 1
    
      With .Selection.Find
        .Text = sOutput(i)
        .Replacement.Text = Range(sInput(i))
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
      End With
    
    Next
    
    End With
    
    wrdDoc.PrintOut

    wrdDoc.Close False

    wrdApp.Quit False
    
    Set wrdDoc = Nothing
    Set wrdApp = Nothing

End Sub

Macro 2:

Code:
Sub Replacing()

    Dim sFile   As String

    sFile = "Pack"

Set objword = CreateObject("Word.Application")
objword.Visible = True

Set objdoc = objword.Documents.Open("C:\Users\Admin\Desktop\" + sFile + ".doc")
Set objSelection = objword.Selection

objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True

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

objSelection.Find.Execute , , , , , , , , , , wdReplaceAll

objSelection.Find.Text = "NAME2"
objSelection.Find.Replacement.Text = Range("C3")

objSelection.Find.Execute , , , , , , , , , , wdReplaceAll

objSelection.Find.Text = "Address Line 1"
objSelection.Find.Replacement.Text = Range("C8")

objSelection.Find.Execute , , , , , , , , , , wdReplaceAll

objSelection.Find.Text = "Address Line 2"
objSelection.Find.Replacement.Text = Range("C9")

objSelection.Find.Execute , , , , , , , , , , wdReplaceAll

objdoc.PrintOut
objdoc.Close SaveChanges:=False
objword.Quit

End Sub

Many thanks and feel free to use yourselves if this is something your looking for.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
BUMP, could one person please help its a simple check of two macros to see which is better.

And hopefully help add some events and screen updating so the user just sees a progress or wait notification while its running to avoid macro crash and maybe some error logging to end the macro.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,671
Members
452,937
Latest member
Bhg1984

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