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.