Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document = DocumentModel.Load("FindAndReplaceContent.docx")
' The easiest way how you can find and replace text is with "Replace" method.
document.Content.Replace("%INSPECTION_CREATED_ON%", Now.ToShortDateString)
document.Content.Replace("%MLS%", frmMain.statbar_MLS.Text)
document.Content.Replace("%INSPECTION_ADDRESS%", frmMain.statbar_ClientAddress.Text)
document.Content.Replace("%INSPECTION_COUNTY%", "County")
document.Content.Replace("%INSPECTION_HOME_TYPE%", "Home Type")
document.Content.Replace("%INSPECTION_ARCHITECTURES%", "Architectures")
document.Content.Replace("%INSPECTION_HOME_BUILT%", "Home Built")
document.Content.Replace("%SQUARE_FOOTAGE%", "Square Footage")
document.Content.Replace("%INSPECTION_DATE_TIME%", "Inspection Date")
' Find an image placeholder.
Dim picturePlaceholder = document.Content.Find("%Image1%").first()
Dim picture As New Picture(document, "C:\Users\RayVi\OneDrive\Pictures\company_logo.png")
' Replace the placeholder text with image.
picturePlaceholder.Set(picture.Content)
document.Save("FoundAndReplacedContent.docx")
End Sub
The document has 5 occurrences of "%Image1% text, you can replace all of them like this:
Dim picture As New Picture(document, "C:\Users\RayVi\OneDrive\Pictures\company_logo.png")
For Each picturePlaceholder In document.Content.Find("%Image1%")
picturePlaceholder.Set(picture.Content)
Next
Or if you want to target just those content controls, then you could try this:
For Each contentControl In document.GetChildElements(True).OfType(Of IContentControl)()
If contentControl.Properties.Title = "%Image1%" OrElse contentControl.Properties.Tag = "%Image1%" OrElse contentControl.Content.ToString().Contains("%Image1%") Then
contentControl.Content.Set(picture.Content)
If contentControl.ContentControlType = ContentControlType.Picture Then contentControl.Properties.IsShowingPlaceholderText = False
End If
Next
I guess we are getting the same results as before with images not showing. I found out why…
Apparently, under “Show document content” I needed to uncheck the “Show picture placeholders”. And now the images appear.
However, the contentControl In document.GetChildElements(True).OfType(Of IContentControl) gets an error of Type ‘IContentControl’ is not defined.
OfType is an extension method from System.Linq, I’m afraid that I was unable to reproduce your issue.
Can you please send us a small Visual Studio project that reproduces this so that we can investigate it?