index update and export failures in macros

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ratbert
Posts: 5
Joined: Thu Jan 24, 2008 1:48 pm

index update and export failures in macros

Post by ratbert »

Hi,

I have an issue with a OOBASIC macro I have installed in OOo 2.3.1 for Writer on WinXP:

Code: Select all

Sub SaveAsPDF( cFile )
   cURL = ConvertToURL( cFile )
   ' Open the document. Just blindly assume that the document 
   ' is of a type that OOo will correctly recognize and open 
   ' without specifying an import filter.
   oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, _
            Array(MakePropertyValue( "Hidden", True ),))

   ' update all indexes
   oIndexes = oDoc.getDocumentIndexes()
   for i = 0 to oIndexes.getCount () - 1
      oIndexes (i).update
   next i
      
   ' save updated file
   oDoc.storeAsURL( cURL, Array () )

   ' calculate the PDF file name
   'cFile = Left( cFile, Len( cFile ) - 4 ) + ".pdf"
   'cURL = ConvertToURL( cFile )
   
   ' Save the document using a filter.   
   'oDoc.storeToURL( cURL, Array(_
   '         MakePropertyValue( "FilterName", "writer_pdf_Export" ),)
   
   oDoc.close( True )
End Sub

Function MakePropertyValue( Optional cName As String, Optional uValue ) _
   As com.sun.star.beans.PropertyValue
   Dim oPropertyValue As New com.sun.star.beans.PropertyValue
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function
The idea of the macro which I have installed in .Standard is to open the specified file, update it's tables and indexes and then export it as PDF. As you can see I have the PDF bits commented out at the moment. This script is not mine - I borrowed bits from various web pages. When I run the macro from a Cygwin command line on Win XP, it reports no errors:

/cygdrive/c/Program\ Files/OpenOffice.org\ 2.3/program/soffice.exe -invisible 'macro:///Standard.Export2PDF.SaveAsPDF(u:\l1ifman.odt)'

However the script doesn't quite work out as expected. Researching the issue, it seems that because the doument is likey to be reflowed/repaginated after loading as an asynchronous process, this indexes get updated with inaccurate page numbers (many headings on the same page typically), and the export step generates a PDF which contains a lot of contiguous blank pages (with headers and footers though) where presumably a step change in the reflow moved the data to a new page while the export was being done.

All of this maybe exasperated by the fact I have around 90 OLE OO draw objects in my writer document (all in frames), and maybe even more so because my ~200 page doc has an extra ~100 pages added to it by a Perl script (auto code documentation) bumping it up to over 300 pages.

It seems to me that it is buggy behaviour that "update-index" and "export" like operations can create incorrect results. An asynchronus relow is useful functionality that makes editing much faster after a load, but operations that rely on the results of a reflow ought to wait until the reflow is complete shouldn't they? Possibly, they ought to be able to expidite the reflow so that the results can be obtained quicker, but that is a seperate issue.

I did see some java related workaround using some kind of refresh() call. I added an oDoc.refresh() to the macro above, but it had no effect on my problem. Probably the wrong function to call I guess. I can't find a workaround for a Basic Macro mechanism to pause until the document reflow is complete.

Anyway, Is this a bug I should file (Filed now, as 86024)? Is there a work around that works in OOBASIC? I'll be happy to put together a demo document (with non sensitive contents) to demonstrate the problem if it helps. Since this jeopardises the whole of my current project, I'll be more or less happy to do just about anything ;-) (Example doc now attached to issue 86024).

Thanks,
Ratbert
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand

Re: index update and export failures in macros

Post by probe1 »

Looking at your code...


I think, you should use a UpdateDocMode , see:
Update Links on Open

And maybe the usage of some WAIT statements helps with timing problems.

Good luck
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
dc2
Posts: 2
Joined: Wed Mar 12, 2008 11:42 am

Re: index update and export failures in macros

Post by dc2 »

I have exactly the same issue.
That is to say : I have a huge odt document (~700 pages)
I open it in Open Office.
Then I insert a Table of Content I update it and the page don't match the real one.
It looks like it didn't take into account the TOC I've just inserted. The TOC is 20 pages long.
And I have a 20 pages difference between the reference in the TOC and the real one.

I try several work around : ReUpdate the TOC, go to the end of the document, wait several seconds... no one works.
Actually, I came to the same conclusion : the repagination has not ended when I update the TOC

I've just found a work around to recompute the number of page.
I place the visible cursor at the start of the document and I jump to the next page until I reach the end of the doc. Then I reupdate the TOC
It takes some time but it seems to work.


I hope this can help.

My code in VB :

Code: Select all

oIndex.Update
Dim oVisibleCursor As Object
Set oVisibleCursor = objDocument.currentcontroller.ViewCursor
oVisibleCursor.gotoStart False
    
Dim bNextPageFound As Boolean
bNextPageFound = True
Do
  bNextPageFound = oVisibleCursor.JumpToNextPage
Loop While bNextPageFound
        
oIndex.Update
oVisibleCursor.gotoStart False
Win XP SP2
OOo 2.3.0
Post Reply