I'm trying to take a word document and populate placeholders but using the Find
and ReplaceAll
method (which works just fine) and then copy in a table from excel into the the word document - The intent is to find the placeholder for the table, then replace it but I'm coming undone in getting the table in the correct place (replacing the place holder).
Here's my code:
Sub Test()
Dim i As Long, iManager As Long, iFunds As Long
Dim sM As String
Dim WS As Worksheet, WS1 As Worksheet
Dim sManager() As String
Dim objWord
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set WS = ThisWorkbook.Sheets("Investment Manager details")
Set WS1 = ThisWorkbook.Sheets("Fund details")
Set WS2 = ThisWorkbook.Sheets("Worksheet")
ReDim sManager(1 To (WS.Range("A" & WS.Rows.Count).End(xlUp).Row - 1), 1 To 2)
For iManager = 1 To UBound(sManager)
sM = iManager
sManager(iManager, 1) = WS.Range("A" & iManager + 1).Value
sManager(iManager, 2) = WS.Range("B" & iManager + 1).Value
iFunds = WorksheetFunction.CountIf(WS1.Range("H:H"), sManager(iManager, 1))
objWord.Documents.Open "C:\Users\Jeremy\Documents\Manager Documents\Template.doc"
With objWord.ActiveDocument.Content.Find
.Text = "%Manager.Name%"
.Replacement.Text = sManager(iManager, 1)
.Replacement.ClearFormatting
.Replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
WS1.Range("C50:D52").Copy 'This is just for example - the actual table will vary in size so can be included in the template
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="Table"
objWord.Selection.PasteExcelTable False, False, False
Next
iManager = 0
objWord.Quit
Set objWord = Nothing
End Sub
How can I get the placeholder and put the pasted table in the right place?