|
|
OpenOffice Macros: Open, Save and Close a FileHow to Use a Macro for Opening, Saving and Closing a DocumentOne 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 DocumentOne 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 FileThe 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 FileSaving 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 FileOpening and saving a file(as shown above) are really easy, and closing one is even easier: Doc.Close (True)
ConclusionIn 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 ReadingOpenOffice.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.
Comments
Oct 14, 2008 5:42 AM
Guest :
Oct 19, 2008 10:26 AM
Guest :
Oct 24, 2008 3:53 PM
Mark Alexander Bain :
3 Comments
|
|
|
|
|
|
|
|