I insert an OLE object in a Word document using the VBA code below.
Sub Test()
Selection.InlineShapes.AddOLEObject ClassType:="Excel.Sheet.12", FileName _
:="C:\Users\ananyroy\Documents\SDWAN\NRFUAutomation\Trials\TestFile.xlsx", LinkToFile _
:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{90140000-0011-0000-0000-0000000FF1CE}\xlicons.exe" _
, IconIndex:=100, IconLabel:="Book1.xlsx", Range:=ActiveDocument.Paragraphs(4).Range
End Sub
I am looking for a method to add this OLE object after a particular string or in place of the placeholder string.
The Range parameter seems a tedious way to achieve it.
Try this:
Sub Test()
Dim rng As Word.Range
Set rng = ActiveDocument.Content
With rng.Find
.ClearFormatting
.Forward = True
.Text = "Placeholder Text"
.Wrap = wdFindStop
.Execute
If .found Then
rng.InlineShapes.AddOLEObject ClassType:="Excel.Sheet.12", FileName _
:="C:\Users\ananyroy\Documents\SDWAN\NRFUAutomation\Trials\TestFile.xlsx", LinkToFile _
:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{90140000-0011-0000-0000-0000000FF1CE}\xlicons.exe" _
, IconIndex:=100, IconLabel:="Book1.xlsx", Range:=rng
End If
End With
End Sub