OpenOffice.org Macros: Working with Hidden Files

How to Use OpenOffice.org Macros to Open and Hide Documents

Use a Macro to Hide a Document - Mark Alexander Bain
Use a Macro to Hide a Document - Mark Alexander Bain
Macros can be useful when it comes to processing lots of files. This article shows how a macro can be used to open a document without the user seeing anything at all.

As OpenOffice.org macro programmers develop their scripts they will need to open and close documents; for example they may want to:

  • open a Calc file to update a spreadsheet
  • open up a Writer file in order to add some text

However, any users of those macros will not want to see lots of documents opening and closing all over their screens - they will just want to run the macros and then see the end result. The solution is, of course, to somehow run the macro without displaying any documents that need to be opened. Therefore this article shows a macro developer how to:

  • open a document, but keep it hidden
  • close the document once the macro's finished with it

It's worth noting that there are two ways of keeping a document hidden when accessing it from a macro:

  • on some versions of Linux it is possible to hide the frame containing the document
  • in all macros (whether Windows or Linux) it is possible to set one of the document's property values so that it remains hidden

Using Frames to Keep a Document Hidden

The OpenOffice.org loadComponentFromUrl is used to open documents, and one of the parameters it requires is the name of the frame into which the document is to opened. Normally this is the default "_blank" (for a new frame), but some versions of Linux allow the "_hidden" frame to be used, for example:

Sub hidden_frame (Url as String)
Dim Doc
'Open the document in the _hidden frame
Doc = starDeskTop.loadComponentFromUrl (Url, "_hidden", 0, array())
'Close the document
Doc.Close (True)
End Sub

However, in many cases "_hidden" is treated the same as "_blank". It is, therefore, more reliable to use the document's property values to hide the file.

Using Property Values to Keep a Document Hidden

As well as the name of the frame, the loadComponentFromUrl method expects an array of property values to be passed to it. In order to hide a document the hidden property value must be included in the array and set to TRUE, for example:

Sub hidden_property (Url as String)
Dim Doc
'Create an array of property values
Dim args(0) As New com.sun.star.beans.PropertyValue
'Set the property value to be Hidden=TRUE
args(0).Name = "Hidden"
args(0).Value = TRUE
'Open the document, but keep it hidden
Doc = starDeskTop.loadComponentFromUrl (Url, "_blank", 0, args())
'and close the file again
Doc.Close (True)
End Sub

Conclusion

This article has shown just how easy it is to hide an OpenOffice.org document while working on it with a macro; of the two techniques the hidden frame is easier to use but cannot be guaranteed to work with every OpenOffice.org installation; however, whilst the property value method is slightly more complicated to use, it will always work regardless of the version of OpenOffice and won't be affected by the user's choice of Linux or Windows.

Further Reading

OpenOffice Macros: Open, Save and Close a File

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement

Comments

Oct 24, 2008 7:27 PM
bobban :
Another very useful article, thanks Mark!
1
Advertisement
Advertisement