at first to the background what I try to achieve. I am basically creating a report and depending on the user selecting export it to different formats (odt, doc & pdf).
So my approach is that I generate the whole document in the open document format odt (Which is very neat after you worked your way through the specification and XML), then I use the openoffice com interfaces to open the document programmatically and save it in word or in pdf.
This works perfect so far but I have the problem that the table of content doesn't get updated.
It doesn't matter so much in the DOC format, because the user can do it manually afterwards, but in PDF the user doesn't get this option.
I recorded the macro of the TOC update and tried to use it but somehow it doesn't work. I doesn't give me an error message but it just doesn't fire.. Below is the Makro:
sub Main
dim document as object
dim dispatcher as object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:UpdateCurIndex", "", 0, Array())
end sub
Basically I use this to create it:
oDispatcher := fOpenOffice.createInstance('com.sun.star.frame.DispatchHelper');
oDispatcher.executeDispatch(fDocument.CurrentController.Frame
, '.uno:UpdateCurIndex', '', 0
, VarArrayCreate([0, 0], varVariant));
with those variants:
fOpenOffice := CreateOleObject('com.sun.star.ServiceManager');
wProperties := VarArrayCreate([0, 0], varVariant);
wProperties[0] := MakePropertyValue('Hidden', True);
fDocument := fDesktop.loadComponentFromURL('file:///' + FileName
, '_blank', 0, wProperties);`
Is there anything I forgot in the process? I haven't listed the whole source code which is pretty standard and works perfect. It is just those two lines with the "oDispatcher" which don't do the job.
Ok, now I found out about the problem and come up with an work around! 1. The update of the Table of Contents only works when the documnet is opened NOT hidden! So I had to change my code to:
I used a very simple and efficient work around by adding a global Makro to OpenOffice which will automatically execute my Makro when a document is opened. All the Makro is doing is to look the text "Content", move one line down and update the selected Table of content. But this only works when openOffice ist started visible to he user. Otherwise it doesn't work. The script for updating it is below: