Images inserted into docm and docx can not dislay after running macro ActiveDocument.Fields.Update

Hi ,

For Word 2007, insert images from files into docm and docx documents through API DocumentBuilder.insertImage, and then running below macro. The inserted images can not display well, instead display cross marks:

ActiveDocument.Fields.Update

After investigation, found that the images were insert into docm and docs files as below fields, not real images:

{INCLUDEPICTURE \d "ooxWord://word/media/image1.png" * MERGEFORMATINET}
{INCLUDEPICTURE \d "ooxWord://word/media/image1.png" * MERGEFORMATINET}

Running the macro may delete the “media” folder of docm and docx. So cross mark was displayed there instead.

But for doc format, the images were inserted into as real images, so there no problems with doc files.

For you convenience, I pasted the code:

Main.java

public static void main(String[] args) throws Exception {
    String out1 = "files\out1.doc";
    String out2 = "files\out2.docm";
    String out3 = "files\out3.docm";
    String styleDoc = "files\test.dot";
    Document doc = new Document(styleDoc);
    DocumentBuilder docBuilder = new DocumentBuilder(doc);
    //insert images
    Shape shape1 = docBuilder.insertImage("files\OLE0.png");
    shape1.setWrapType(WrapType.INLINE);
    docBuilder.writeln();
    Shape shape2 = docBuilder.insertImage("files\OLE1.png");
    shape2.setWrapType(WrapType.INLINE);
    //save documents
    doc.save(out1);
    doc.save(out2);
    doc.save(out3);

    //run macro for out2.docm
    String command = "cscript files\runmacro.vbs " + new File(out2).getAbsolutePath() + " peUpdateFields";
    System.out.println(command);
    Process proc = Runtime.getRuntime().exec(command);
    proc.waitFor();
}

runmacro.vbs:

’ ignore errors
On Error Resume Next

’ start only if 2 arguments are provided 
if WScript.Arguments.Count <> 2 then
wscript.quit
end if

Set word = CreateObject("Word.Application")
word.Visible = TRUE

Set doc = word.Documents.Open( Wscript.Arguments(0))
if doc <> nul then
word.Run( Wscript.Arguments(1))
word.ActiveDocument.Save
end if

word.quit 

The test.dot file just contian an macro peUpdateFields:

’ Updates all the fields in the document
Public Sub peUpdateFields()
ActiveDocument.Fields.Update
End Sub

Is it a defect of Aspose.Words library? Are there any ways to solve this issue?

Thanks.

Hi
Thanks for your inquiry. I cannot reproduce the problem on my side. As I can see Aspose.Words inserts images as embedded.
Could you please attach your output document here for testing? I will check the issue and provide you more information.
Best regards,

I attached all source code and out put files.

You just open out2.docm and will see the issue.

BTW, my Aspose.Words version is 2.5.0.

Thanks.

Hi
Thank you for additional information. I used the latest version of Aspose.Words for testing and all images are includes as embedded. So, please try using the latest version. You can download it from here:
https://releases.aspose.com/words/java/
Best regards,

Can fix it in version 2.5.0?

Thanks

Hi
Thanks for your inquiry. As an option, you can use DOC format instead of DOCX. In this case, your images will be surely included as embedded.
Have you tried with newer version of Aspose.Words? Did this resolve your issue?
Best regards,

I tried the newer version, it can resolve my issue.

But we must support docm and docx format for our customers. It is very helpful if the defect can be fixed in the older version.

Thanks.

Hi Vincent,

Thanks for your request. I am afraid, I cannot suggest you any way to resolve this with old version of Aspose.Words. You should consider updating to the newer version of Aspose.Words.
Best regards,