Jacob - Ms Office

From Wiki dot Concept-In

Jump to: navigation, search

If you have comments or additional information, please add them to the discussion page.

Contents

Introduction

The information about COM is scattered everywhere and it is very difficult to find useful exemples of COM usage through Java (it is only slightly easier to find useful exemples of COM usage at all). Jacob is very powerful but it is also very generic. It allows to drive any COM provider, it is therefore not very user friendly when it comes to driving Ms Office.

In this page I will store the bits and pieces of recipe that I use to drive Ms Office Documents. It will therefore _NOT_ be a complete reference or anything. Simply a list of (hopefully) useful recipes.

References

These are some useful sites regarding Jacob usage, how to drive Ms Office applications with Jacob, and Office COM References.

Tutorial: Jacob & Microsoft Word by Kai Ruhl

Also there is a lot to be read in the documentation which is distributed with the jacob project itself.

Calling Ms Word macros

Without arguments

First let's open the document :

ActiveXComponent word=this.getWordInstance();
Dispatch documents = word.getProperty("Documents").toDispatch(); 
Dispatch document = Dispatch.call(documents, "Open", "c:\myworddoc.doc").toDispatch();

(note that I didn't include any error handling code to avoid bloating the exemple.)

I will now call a macro which is stored in "Normal.dot":

Dispatch.call(word, "Run", new Variant("Normal.Module1.TestMacro"));

If I just created a macro in my word document, not changing any of the default names and calling my macro "test", i could call it as such :

Dispatch.call(word, "Run", new Variant("Project.NewMacros.test"));

Keep in mind that these are default names, you may have to adjust them for your particular case.

With arguments

Calling a macro with arguments is not much more difficult:

Dispatch.call(word, "Run", new Variant("Normal.Module1.TestMacro"), new Variant(arg1),new Variant(arg2),..);

The Jacob API (1.10) defines "call" method signatures with up to 8 arguments so you can call macros that take up to 7 arguments with this signature. If you want/need more you will have to use :

public static Variant callN(Dispatch dispatchTarget,
                            String name,
                            Object[] args)

the Dispatch.call methods are only utility wrappers for callN as far as I understand it (which is not very far :) )

More on macros in MS Word

I have recently been told that you can have template files load automatically when you start word, in addition to Normal.dot. This allows for a better isolation of the macros for different projects. If you place a .dot file in %HOME%\Application Data\Microsoft\Word\STARTUP it will automatically be loaded when word starts up.

This led me to try and call a macro loaded in such a way. I used mytemplate.dot which contains the module Module1 which contains my macro Test1. A successfull call for me went like this (if your macro doesn't use arguments you can of course remove them...):

Dispatch.call(word, "Run", new Variant("Module1.Test1"), new Variant(arg1),new Variant(arg2),..);

Which tends to indicate that I wouldn't need to prefix my other macros call in word with Normal or project. I haven't tried it, however I can already tell that in MS Excel I had to prefix the macro with the file name or the call would fail.

Calling Ms Excel macros

There is only one notable difference between calling Ms Excel and Ms Word macros: Ms Excel does not have the equivalent of the "Normal.dot" file. Thus you have to give the precise location of the macro, you can't cheat and put it in "normal.dot". This is done like so:

Variant result = Dispatch.call(excel, "Run", new Variant("test.xls!Module1.test"), 
                                             new Variant(arg1), 
                                             new Variant(arg2));

As you can see, the project namespace is repalced by the filename, but that's the only difference. I didn't look further into this so I don't know if there is a place where the namespaces for macros in the different office applications are explained. The information is probably lost somewhere in the huge mess that is MSDN.

Personal tools