开发者

Add VBA script to Office Word docs

Is there a library in Java开发者_高级运维, that allows adding pre-written VBA code into a document ? I have a lot of files, and I need the same code to be inserted in all of the documents.

I have a test with checkboxes, I gave the test to the people who are supposed to answer that, and later the idea of calculating the scores came up to mind and wrote a little of VBA code, now I need to include that on all the test documents once I get them back.

I'm looking for a Java library, but if there is a simpler way to do that in Delphi or VB, that would be great !

Thanks in advance, Regards, Taha


Java COM Automation with Jacob and JBuilder (contains Word example)

There's J-Integra (not free): Automating Microsoft Word with Java (and J-Integra)

If you C# (or VB.NET), it's going to be much easier (plus there are thousands of examples): How to automate Microsoft Word to create a new document by using Visual C#


I've injected a macro into an existing docx using docx4j (Java), and via a VSTO Word add-in (C#, OpenXML SDK).

Here is the docx4j code (not tested recently):

// Add our macro to the document

// Get vbaProject.bin, and attach it to wordDocumentPart

java.io.InputStream is = ResourceUtils.getResource("docm/vbaProject.bin");                  
VbaProjectBinaryPart vbaProject = new VbaProjectBinaryPart();
vbaProject.setBinaryData(is);
wordDocumentPart.addTargetPart(vbaProject);

// Get /word/vbaData.xml, and attach it to vbaProject
VbaDataPart vbaData = new VbaDataPart();
java.io.InputStream is2 = ResourceUtils.getResource("docm/vbaData.xml");                    
vbaData.setDocument( is2 );

vbaProject.addTargetPart( vbaData);     

// Change the Word document's content type!
wordDocumentPart.setContentType( new ContentType(
        ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED ) );
ContentTypeManager ctm = p.getContentTypeManager();
PartName partName = wordDocumentPart.getPartName();

ctm.removeContentType( partName  );
ctm.addOverrideContentType( new java.net.URI("/word/document.xml"), 
        ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);


I think you might be making this harder than it has to be. Unless I'm misunderstanding and you won't be using the documents on your computer, it doesn't matter that the documents you'll be getting back don't contain any code. If you have the code saved in a Word template, you just need to add that template to your startup directory for Word and your macros will automatically load when you launch Word. Unless you changed its location, it should be the following folder:

%APPDATA%\Microsoft\Word\STARTUP\

If you don't have the code saved in a template, that's fine. Open the Word document that has the code, hit F12 (or go to 'Save as'), and change the 'Save as type' field to Document Template. By default, Word will change the directory to the folder where the Normal.dot template is stored. Simply change it to the STARTUP folder I referenced above and save it (or save it to your desktop and move it there afterwards). To edit the code later on, right-click on the file and choose 'edit.' (By default, Windows will launch a new Word doc using the template's settings if you just double-click on it.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜