Excel VBA not executing in Word

smit3446

New Member
Joined
Nov 16, 2015
Messages
44
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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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