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:
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.
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: