Using INCLUDEPICTURE in .docx documents

Hi, all,

We are experiencing problems with .docx documents due to Word’s bug with INCLUDEPICTURE. We use INCLUDEPICTURE combined with nested MERGEFIELD, like this:
{ INCLUDEPICTURE "{ MERGEFIELD "vpt_digitale_ht" }" * MERGEFORMAT \d }
But, when we try to save the document all the fields are lost and replaced with error image. Seemingly, this is known Word bug.

We would like to use .docx document later for mail merge using Aspose.Words as we do it successfully with .doc documents.

Since your domain knowledge related to merging and Word itself is greater, do you have an idea about the possible workaround? Do you, maybe, provide any kind of solution for this problem? How can we still “use” INCLUDEPICTURE in .docx?

Thanks,
Jelena

Hi Jelena,

Thanks for your inquiry and sorry for the delayed response.

The reason why such a setup can not be merged is because INCLUDEPICTURE fields are imported into Aspose.Words’ DOM as Shape objects. This allows you to access image properties but at the same time restricts any extra parameters of the field, in this case the included merge field. You can however still merge INCLUDEPICTURE fields which contain merge fields by using Shape.SourceFullName property. Any nested field in INCLUDEPICTURE appears through the SourceFullName property. Using this we can parse the field name and insert a proper mergefield field into the model at any location which latest on can be merged using mail merge.

I hope, this explains the scenario.

Best Regards,

Hi,

In addition, you can also use LoadOptions.PreserveIncludePictureField property to override the default behaviour and if you need the INCLUDEPICTURE field to be preserved, for example, if you wish to update it programmatically. Please note, however that this approach is not common for Aspose.Words you should use it on your own risk.

One of the possible use cases may be using a MERGEFIELD as a child field to dynamically change the source path of the picture. In this case you need the INCLUDEPICTURE to be preserved in the model.

Best Regards,

Hi, Awais,

Thank you for such a comprehensive answer. However, we’ve already managed to show pictures combining INCLUDEPICTURE with MERGEFIELD values. But, only in Word 97 - 2003 (.doc) document format.

There is a known problem with using INCLUDEPICTURE in Word 2007 (and later) .docx files. It is not possible to save .docx document containing INCLUDEPICTURE field. It is instantly replaced by an image, there is no more link to it. In the following link, it is said that Microsoft does not have an intention to fix it. Now it’s like that by design.
http://social.technet.microsoft.com/Forums/en-US/word/thread/9d2df488-2eb7-4743-b10d-0161b38ff143/

What we need is to be able to insert images into a document dynamically during mail merge. But, apparently, we can’t use INCLUDEPICTURE mechanism anymore.
So, we wanted to know if there is any workaround that you provide. How can we define it inside of .docx file, so we can later insert picture dynamically using Aspose.Words’ mail merge to get its value?

Best regards,
Jelena

Hi Jelena,

Thanks for the additional information.

Yes, you’re right; it seems to be a bug in Microsoft Word. However, you can still convert your DOC files to DOCX format by using the following code snippet and still preserving those INCLUDEPICTURE fields in DOCX:

LoadOptions options = new LoadOptions();
options.LoadFormat = LoadFormat.Doc;
options.PreserveIncludePictureField = true;
Document doc = new Document(@"c:\temp\in.doc", options);
doc.Save(@"C:\temp\out.docx");

Moreover, I would suggest you please read the following article on inserting images during Mail Merge:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/

I hope, this will help.

Best Regards,

Hi, Awais,

I’ve tried converting from .doc to .docx using Aspose, and it really worked nicely, preserving INCLUDEPICTURE. Problem with this approach is that user himself cannot create .docx template that we would use for merge which wouldn’t be very convenient.

However, the link you suggested was very helpful because it revealed that we can easily tag image merge fields with Image: prefix. Since we cannot use INCLUDEPICTURE in .docx in any way, this is pretty simple and clear solution to use instead.

So, only:
{ MERGEFIELD "Image:vpt_digitale_ht" }
instead of:
{ INCLUDEPICTURE "{ MERGEFIELD "vpt_digitale_ht" }" * MERGEFORMAT \d }
does the work.

Thanks a lot.

Best regards,
Jelena

Hi Jelena,

Thanks for the additional information. Sure, you can specify a field name prefix, for example “Image:MyFieldName” in the document to be able to directly merge images during Mail Merge. Please let me know if I can be of any further assistance.

Best Regards,

A post was split to a new topic: Using MERGEFIELD and INCLUDEPICTURE fields inside IF field structure