jessebh2003
Board Regular
- Joined
- Feb 28, 2020
- Messages
- 71
- Office Version
- 365
- Platform
- Windows
I'm writing an Excel macro that would open a Word template and replace the merge fields with data from a specific cell in Excel. I've been searching the internet for weeks and have been mostly unsuccessful. Pulling from what I could find, I came up with the below. However, this didn't populate the merge fields.
For context, the Excel data is exported from our tracking system and then a summary macros is ran. The below macro would run second. Can someone help me figure out how to find a merge field in the template and replace it with the data from Excel? I'm really struggling with this one. Thanks.
For context, the Excel data is exported from our tracking system and then a summary macros is ran. The below macro would run second. Can someone help me figure out how to find a merge field in the template and replace it with the data from Excel? I'm really struggling with this one. Thanks.
VBA Code:
' Starts a Word document
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add(Template:="SomeWordTemplate.dotx", _
NewTemplate:=False, DocumentType:=0)
With wrdDoc
.Application.Selection.Find.Text = "<<MergeFieldName1>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B5")
.Application.Selection.Find.Text = "<<MergeFieldName2>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C5")
.SaveAs2 Filename:=("NewFileName"), _
FileFormat:=wdFormatDocumentDefault, AddtoRecentFiles:=True
End With
End Sub