I have a Word document where I would like to locate a particular string of text (just regular text, nothing special about it) and replace it with a rather long, complex IncludePicture MergeField.
The actual text of the IncludePicture MergeField is the following:
{ IF { INCLUDEPICTURE { IF TRUE { MERGEFIELD ClientLogo } } \x \y } { INCLUDEPICTURE { IF TRUE { MERGEFIELD ClientLogo } } \x \y } }
The reason for the complex string is that it is required for us to do our MailMerge and insert an image successfully.
I’ve attached a sample Word document. In it, there’s the text “CLIENTLOGO” near the bottom of the document. What I’d like to do is replace that text with the string I’ve provided above as a MergeField (not just a simple search-and-replace of text, that won’t work with a MailMerge to insert an image).
Any assistance would be greatly appreciated! Thank you.DynamicImage-Textbox-Aspose-Example-ClientLogo.zip (24.3 KB)
@Robert343,
Thanks for your inquiry. In your case, we suggest you please implement IReplacingCallback interface. You need to write code to insert MergeField field in IReplacingCallback.Replacing. Please refer to the following article.
Find and Replace
Please use DocumentBuilder.InsertField to insert the field in the document. If you face any issue, please ZIP and attach your expected output Word document here for our reference. We will then provide you more information about your query.
Thank you for the prompt response.
I followed your advice and think I might be close but I do not know how I’ll recreate that complicated field I have using the MergeField objects.
My current code is below (you can use the template from my original post).
var findReplaceOptions = new FindReplaceOptions
{
ReplacingCallback = new FindAndReplaceClientLogos()
};
wordTemplateDocument.Range.Replace("CLIENTLOGO", string.Empty, findReplaceOptions);
// save the template after changes
wordTemplateDocument.Save(mergeTemplateFullName);
internal class FindAndReplaceClientLogos : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
Node currentNode = e.MatchNode;
DocumentBuilder docBuilder = new DocumentBuilder(e.MatchNode.Document as Document);
docBuilder.MoveTo(currentNode);
docBuilder.InsertField(@"{ IF { INCLUDEPICTURE { IF TRUE { MERGEFIELD DynamicImage1 } } \x \y } { INCLUDEPICTURE { IF TRUE { MERGEFIELD DynamicImage1 } } \x \y } }");
currentNode.Remove();
// signal to the replace engine to do nothing because we have already done all that we wanted
return ReplaceAction.Skip;
}
}
That code will give me an exception upon hitting the “docBuilder.InsertField()” line: Field code ‘{ IF { INCLUDEPICTURE { IF TRUE { MERGEFIELD DynamicImage1 } } \x \y } { INCLUDEPICTURE { IF TRUE { MERGEFIELD DynamicImage1 } } \x \y } }’ is invalid or not supported.
I was hoping I could just use the string to create the field but I guess that doesn’t work and I’ll need to create it using individual objects, like docBuilder.InsertField(FieldType.FieldIf), docBuilder.InsertField(FieldType.FieldIncludePicture), etc.
Am I correct in this? If so, do you think you could demonstrate how I’d accomplish creating that big MergeField string I have? I’d be greatly appreciative.
@Robert343,
Thanks for your inquiry. We are working over your query and will get back to you soon.
@Robert343,
Please use the following code snippet to insert the nested fields in the document. Hope this helps you.
DocumentBuilder docBuilder = new DocumentBuilder(doc);
FieldIf fieldif = (FieldIf)docBuilder.InsertField(FieldType.FieldIf, false);
docBuilder.MoveTo(fieldif.Separator);
FieldIncludePicture fieldpic = (FieldIncludePicture)docBuilder.InsertField(FieldType.FieldIncludePicture, false);
docBuilder.MoveTo(fieldpic.Separator);
fieldif = (FieldIf)docBuilder.InsertField(FieldType.FieldIf, false);
docBuilder.MoveTo(fieldif.Separator);
docBuilder.Write(" True ");
FieldMergeField fieldMerge = (FieldMergeField)docBuilder.InsertField(FieldType.FieldMergeField, false);
fieldMerge.FieldName = "DynamicImage1";