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 = Dispatch.call(allDocuments, "Add").toDispatch();
Dispatch select = wordApp.getProperty("Selection").toDispatch();
Dispatch.call(select, "TypeText", "abc ");
//Change style to "Heading 1"
Dispatch dispStyles = Dispatch.call(document, "Styles").toDispatch();
Variant v = Dispatch.get(dispStyles, "Heading 1");
Dispatch.put(select, "Style", v);
//type def (in new style)
Dispatch.call(select, "TypeText", "def");
but this produces the following exception:
com.jacob.com.ComFailException: Can't map name to dispid: Heading 1
Any ideas what's going wrong?
Thanks for any assistance.