I use docx4j to put macros in documents I create in my application. Following code works great with versions 2.8.1 and lower but doesn't work work with newer versions.
What has changed ? I really need to use new version (3.2.1) because of PDF issues but I need my macros too.
What I do now is I take macros from default template and put them in my new documents:
- Code: Select all
MainDocumentPart wordDocumentPart = output.getMainDocumentPart();
ContentTypeManager ctm = output.getContentTypeManager();
PartName vbaProjectBinaryPartName = new org.docx4j.openpackaging.parts.WordprocessingML.VbaProjectBinaryPart().getPartName();
PartName vbaDataPartName = new org.docx4j.openpackaging.parts.WordprocessingML.VbaDataPart().getPartName();
VbaProjectBinaryPart vbaProjectBinaryPart = (VbaProjectBinaryPart) template.getParts().get(vbaProjectBinaryPartName);
VbaDataPart vbaDataPart = (VbaDataPart) template.getParts().get(vbaDataPartName);
wordDocumentPart.addObject(vbaProjectBinaryPart);
wordDocumentPart.addObject(vbaDataPart);
wordDocumentPart.setContentType(new ContentType(ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED));
ctm.addOverrideContentType(new java.net.URI("/word/document.xml"), ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);
output.save(outFile);
And it works great in v2.8.1, when I switch to 3.2.1 it throws exception like:
- Code: Select all
WARNING: No PartStore configured for this package
org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships of /
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:392)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:168)
at org.docx4j.openpackaging.io.SaveToZipFile.save(SaveToZipFile.java:97)
at pl.com.xxx.wind.client.bs.gui.raport.util.docx.DocxUtilities.saveDocument(DocxUtilities.java:86)
at pl.com.xxx.wind.client.bs.letter.DOCXFactory.PismaProcesoweDOCXFactory.createDOX(PismaProcesoweDOCXFactory.java:200)
at pl.com.xxx.wind.client.bs.letter.DOCXFactory.PismaProcesoweDOCXFactory.createDOCXForIdTypLetterAndIdPaczka(PismaProcesoweDOCXFactory.java:68)
at pl.com.xxx.wind.client.bs.LetterDOCXFactory.createDOCXForIdTypLetterAndIdPaczka(LetterDOCXFactory.java:126)
at pl.com.xxx.wind.client.bs.gui.ManageLettersInternalFrame.createDOCXLetterSent(ManageLettersInternalFrame.java:3862)
at pl.com.xxx.wind.client.bs.gui.ManageLettersInternalFrame.createDOCXLetterSentButtonActionPerformed(ManageLettersInternalFrame.java:6714)
at pl.com.xxx.wind.client.bs.gui.ManageLettersInternalFrame.access$4100(ManageLettersInternalFrame.java:72)
at pl.com.xxx.wind.client.bs.gui.ManageLettersInternalFrame$49.actionPerformed(ManageLettersInternalFrame.java:5397)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
ERROR 2015-07-28 13:56:06,128 (pl.com.xxx.wind.client.bs.gui.ManageLettersInternalFrame:6716) Failed to add parts from relationships of /
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to add parts from relationships of /word/document.xml
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:392)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:442)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:387)
... 46 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Failed to put binary part
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawBinaryPart(SaveToZipFile.java:464)
at org.docx4j.openpackaging.io.SaveToZipFile.savePart(SaveToZipFile.java:420)
at org.docx4j.openpackaging.io.SaveToZipFile.addPartsFromRelationships(SaveToZipFile.java:387)
... 48 more
Caused by: java.lang.NullPointerException
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart.getBytes(BinaryPart.java:202)
at org.docx4j.openpackaging.io.SaveToZipFile.saveRawBinaryPart(SaveToZipFile.java:458)
... 50 more
I really need your help