OpenOffice Macros: Open, Save and Close a File

How to Use a Macro for Opening, Saving and Closing a Document

© Mark Alexander Bain

Aug 4, 2008
One of the most useful things that an OpenOffice.org Basic macro can do is to open, save and close a document. This article shows exactly how to do that.

One of the first things that any new user to OpenOffice.org will want do to is to:

• open existing or create new files

• save the files

• close the files

and each of those is very easy to do manually; but what about someone who wants to do all of that in a macro. Surely that's much more complicated? Well actually it's not - actually it's very simple.

Using a Macro to Open an OpenOffice Document

One of the easiest things to do with a macro is to open a file; however, there is one serious consideration - the file name must be entered in a particular format - as a system independent URL (Uniform Resource Locator); fortunately OpenOffice doesn't expect a user to know anything about that format - instead the user just needs to use the convertToUrl function; so, a Linux user would use something like:

Dim Url as String
Url = convertToUrl("/home/bainm/finances.ods")

whilst a user of the Windows operating system would use:

Dim Url as String
Url = convertToUrl("C:/finances.ods")

Then, regardless of the operating system, the URL can be used to open a named file:

Dim Doc
Doc = starDeskTop.loadComponentFromUrl (Url, "_blank", 0, Array())

Of course, at this point a new user may start wondering about the parameters that have to be entered:

• parameter 1: the URL of the file (obviously)

• parameter 2: the frame; this is normally _blank specifying a new window; but another useful value is _hidden which causes the frame to be invisible in some of the Linux versions of OpenOffice.org (to see how to make any document invisible read OpenOffice.org Macros: Working with Hidden Files); it can also be used to access an existing frame by entering the name of the frame (which can't start with an underscore); and if that's the case then parameter 3 must also be set

• parameter 3: the search algorithm flag; this is normally set to 0 (auto), but if a named frame is used then it needs to set (typically to 23 meaning all). A full list of values can be found at http://api.openoffice.org/docs/common/ref/com/sun/star/frame/FrameSearchFlag.html.

• parameter 4: File Properties; normally this is an empty array, but can be used to load other file types (such as a Microsoft word file), or to open password protected files

Creating a Blank File

The code for creating a blank file is very similar to that for opening an existing one - the only difference is in the url; for a new document to be created the private:factory URL must be used. For example, the following code will create a new Calc document:

Url = "private:factory/scalc"
Doc = starDeskTop.loadComponentFromUrl (Url, "_blank", 0, Array())

Using a Macro to Save a File

Saving a document is similar to opening it - in that a function (in this case the storeAsUrl function) needs to be supplied with some parameters:

• the URL

• the file properties

So, the code to save a file is:

Doc.storeAsUrl (Url), Array ())

Using a Macro to Close a File

Opening and saving a file(as shown above) are really easy, and closing one is even easier:

Doc.Close (True)

Conclusion

In just a few, simple lines of code it is possible to write a macro that can easily create a new file or open an existing one, and then to save it and close it; and if the _hidden parameter is available then it can all be done invisibly.

Further Reading

OpenOffice.org Macros: Working with Hidden Files


The copyright of the article OpenOffice Macros: Open, Save and Close a File in Computer Programming is owned by Mark Alexander Bain. Permission to republish OpenOffice Macros: Open, Save and Close a File in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Post Your Comment
NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
What is 2+9? Incorrect, please resolve x + y!
Comments
Oct 14, 2008 5:42 AM
Guest :
Thanks....
Oct 19, 2008 10:26 AM
Guest :
Nice article Mark. I have had no luck using the value "_hidden" as parameter two for loadComponentFromUrl(). I looked up the documentation and it doesn't mention this:

http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XComponentLo ader.html

Would be nice functionality to have...
Oct 24, 2008 3:53 PM
Mark Alexander Bain :
Thanks for the info - the _hidden parameter is, apparently, only available on some versions of Linux.

I've therefore written another article devoted to hiding documents; that's at http://computerprogramming.suite101.com/article.cfm/openofficeorg_macro_wor king_with_hidden_files
3 Comments