Excel VBA not executing in Word

smit3446

New Member
Joined
Nov 16, 2015
Messages
46
I have a VBA program in Excel that I would like to use to perform a Find and Replace with some values gathered from my Excel doc into a template Word document. The Word doc correctly opens, but does not execute the Find and Replace. Here's my code:

Code:
Sub BioBook()
    Dim path As String
    Dim folder As String
    Dim fileName As String
    Dim directory As String
    Dim Company As String
    Dim event_ As String
    Dim date_ As String
    Dim i As Integer
    Dim reinsurers As Integer
    Dim appWord As Object
    Set appWord = CreateObject("Word.Application")
        
    reinsurers = Range("A3", Range("A3").End(xlDown)).Rows.Count
    Company = Worksheets("Inputs").Range("G3").Value
    date_ = Worksheets("Inputs").Range("G4").Value
    event_ = Worksheets("Inputs").Range("G5").Value
    
    appWord.Visible = True
    appWord.Documents.Open "R:\Reinsurers\2. Bios\Reinsurer Bio Template.docx"
    appWord.Selection.MoveDown Unit:=wdLine, Count:=1
        With appWord.Selection.Find
        .Text = "[Company]"
        .Replacement.Text = Company
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        End With
    appWord.Selection.Find.Execute Replace:=wdReplaceAll
        With appWord.Selection.Find
        .Text = "[Event]"
        .Replacement.Text = event_
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        End With
    appWord.Selection.Find.Execute Replace:=wdReplaceAll
        With appWord.Selection.Find
        .Text = "[Date]"
        .Replacement.Text = date_
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        End With
    appWord.Selection.Find.Execute Replace:=wdReplaceAll

End Sub


Any idea why this isn't working? I grabbed the code directly from a Word macro and then edited it to perform from Excel. Thanks for any help!

EDIT: Just wanted to note that I've added in the Word Object Library in Excel for early binding.
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,223,789
Messages
6,174,571
Members
452,573
Latest member
Cpiet

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