Hi,
Is there a way in docx4j to insert macros?
Justin
public static void main(String[] args) throws Exception {
String inputfilepath = "/home/dev/workspace/docx4all/sample-docs/earth.docx";
String outputfilepath = "/home/dev/docm.docm";
// Open a document from the file system
// 1. Load the Package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart wordDocumentPart = wordMLPackage.getMainDocumentPart();
// Get vbaProject.bin, and attach it to wordDocumentPart
java.io.InputStream is = new java.io.FileInputStream("/home/dev/workspace/demo/vbaProject.bin" );
org.docx4j.openpackaging.parts.WordprocessingML.VbaProjectBinaryPart vbaProject
= new org.docx4j.openpackaging.parts.WordprocessingML.VbaProjectBinaryPart();
vbaProject.setBinaryData(is);
wordDocumentPart.addTargetPart(vbaProject);
// Get /word/vbaData.xml, and attach it to vbaProject
org.docx4j.openpackaging.parts.WordprocessingML.VbaDataPart vbaData
= new org.docx4j.openpackaging.parts.WordprocessingML.VbaDataPart();
java.io.InputStream is2 = new java.io.FileInputStream("/home/dev/workspace/demo/vbaData.xml" );
vbaData.setDocument( is2 );
vbaProject.addTargetPart( vbaData);
// Change the Word document's content type!
wordDocumentPart.setContentType( new org.docx4j.openpackaging.contenttype.ContentType(
org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED ) );
// .. but that's a dumb setter right now :(
org.docx4j.openpackaging.contenttype.ContentTypeManager ctm
= wordMLPackage.getContentTypeManager();
org.docx4j.openpackaging.parts.PartName partName
= wordDocumentPart.getPartName();
ctm.removeContentType( partName );
ctm.addOverrideContentType( new java.net.URI("/word/document.xml"),
org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);
// Save it
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}
Users browsing this forum: Google [Bot] and 18 guests