The tools and free software available for OpenOffice.org become more plentiful every day. The Web site http://ooomacros.org/dev.php contains numerous code examples, including an add-on installer, a Basic add-on, and a library installer. The authors of these works include Didier Lachieze, Danny Brewer, Bernard Marcelly, and Andrew Brown. It is also easy to write your own installer, and you already have enough information to do it.
The macro in Listing 5 copies a single library, whose name may be changed when it is copied. The entire process is very simple and straightforward. The modules in the source library are copied one at a time into the destination library. The final argument determines if the destination library is cleared before the copy begins. If the destination library is not cleared first and it contains modules that do not exist in the source library, those modules will still exist in the destination library after the library is copied .
REM sSrcLib is the name of the source library contained in oSrcLibs REM sDestLib is the name of the source library contained in oDestLibs REM oSrcLibs is the source library container REM oDestLibs is the destination library container REM if bClearDest is True, then the destination library is cleared Sub AddOneLib(sSrcLib$, sDestLib$, oSrcLibs, oDestLibs, bClearDest As Boolean) Dim oSrcLib 'The source library to copy Dim oDestLib 'The destination library to receive the modules in oSrcLib Dim sNames Dim i% REM If there is no destination library then simply return If IsNull(oDestLibs) OR IsEmpty(oDestLibs) Then Exit Sub End If REM Clear the destination library if requested If bClearDest AND oDestLibs.hasByName(sDestLib) Then oDestLibs.removeLibrary(sDestLib) End If REM If there is no source library, then there is nothing else to do If IsNull(oSrcLibs) OR IsEmpty(oSrcLibs) Then Exit Sub End If REM If the source library does not exist, then there is nothing else to do If NOT oSrcLibs.hasByName(sSrcLib) Then Exit Sub End If REM If the destination library does not exist, then create it If NOT oDestLibs.hasByName(sDestLib) Then oDestLibs.createLibrary(sDestLib) End If REM This is where the real fun begins! REM It may seem obvious, but the libraries must be loaded first. REM Common mistake to not load the libraries first! oSrcLibs.loadLibrary(sSrcLib) oDestLibs.loadLibrary(sDestLib) REM Get the source and destination libraries REM Get all of the contained modules that should be copied oSrcLib = oSrcLibs.getByName(sSrcLib) oDestLib = oDestLibs.getByName(sDestLib) sNames = oSrcLib.getElementNames() REM For each module, either add it or replace it For i = LBound(sNames) To UBound(sNames) If oDestLib.hasByName(sNames(i)) Then oDestLib.replaceByName(sNames(i), oSrcLib.getByName(sNames(i))) Else oDestLib.insertByName(sNames(i), oSrcLib.getByName(sNames(i))) End If Next End Sub
Assume that you want to copy a specific library from the application into a document so that you can send it to a friend. The fastest method is to simply use the macro organizer and append the library from the application into the document. Sure, you can write a macro, but some things are done faster manually. If, on the other hand, you need to copy libraries frequently, it makes sense to write a macro to do the work. The macro in Listing 6 accepts the name of a library contained in the application and then copies the library into the current document. Both the code library and the dialog library are copied. The analogous macro to copy a library from the document back to the application is trivial and nearly identical to Listing 6.
Sub AppLibToDocLib(sLibName$) Dim oGlobalLib oGlobalLib = GlobalScope.BasicLibraries AddOneLib(sLibName, sLibName, oGlobalLib, BasicLibraries, True) oGlobalLib = GlobalScope.DialogLibraries AddOneLib(sLibName, sLibName, oGlobalLib, DialogLibraries, True) End Sub