This discussion is archived
3 Replies Latest reply: Feb 17, 2009 11:42 PM by 843804 RSS

Formatting Word documents using Jacob

843804 Newbie
Currently Being Moderated
Hi all,

I am working on a project to generate Word documents from Java code and am using the Jacob bridge. I'm able to do most things I require but am having real trouble with applying a formatting style to a a section of text.

I generally work out the calls to make by creating a macro and converting the code it produces into Jacob calls. Setting the style to "Heading 1" will produce the macro code:

Selection.Style = ActiveDocument.Styles("Heading 1")

I have converted this as follows:

//Open word and set visible
ActiveXComponent wordApp = new ActiveXComponent("Word.Application");
wordApp.setProperty("Visible", new Variant(true));
//add a document
Dispatch allDocuments = wordApp.getProperty("Documents").toDispatch();
Dispatch document =, "Add").toDispatch();
Dispatch select = wordApp.getProperty("Selection").toDispatch();
//type abc, "TypeText", "abc ");
//Change style to "Heading 1"
Dispatch dispStyles =, "Styles").toDispatch();
Variant v = Dispatch.get(dispStyles, "Heading 1");
Dispatch.put(select, "Style", v);
//type def (in new style), "TypeText", "def");

but this produces the following exception: Can't map name to dispid: Heading 1

Any ideas what's going wrong?

Thanks for any assistance.