For example, I want to press “save” and run a macro that will remove all macros from this document before saving.
And very good to clear a call for a save event.
I would suggest you reword the subject to mkake clear that this is about document macros to be cleared with the help of a local (application) macro.
(“How to remove document macros by a macro from my local library?” e.g.)
(I may not soon find the time to consider a probable solution.)
Well, I was interested in the question, because I never had done a related code. Thus I tried it without a prior research concerning probable ready-mades being around. This is what I got:
Sub removeDocumentMacros(Optional pDoc, Optional pPreclude As String)
Const mkPrecluded = "Persist"
If IsMissing(pPreclude) Then pPreclude = mkPrecluded
If IsMissing(pDoc) Then pDoc = ThisComponent
bLibs = pDoc.BasicLibraries
uLibs = Ubound(bLibs.ElementNames)
For i = uLibs To 0 Step -1
i_LibName = bLibs.ElementNames(i)
lPersist = (InStr(i_LibName, pPreclude) + Len(pPreclude))=(Len(i_LibName)+1)
If NOT lPersist Then
i_Lib = bLibs.getByName(bLibs.ElementNames(i))'(j)'.getByName(Each bLib In bLibs
uModules = Ubound(i_Lib.ElementNames)
For j = uModules To 0 Step -1
j_ModuleName = i_Lib.ElementNames(j)
mPersist = (InStr(j_ModuleName, pPreclude) + Len(pPreclude))=(Len(j_ModuleName)+1)
If NOT mPersist Then
j_Module = i_lib.getByName(j_ModuleName)
REM Here a processing of the contents of the module might be needed dependng on....
REM Then only if a calculated condition holds:
i_Lib.removeByName(j_ModuleName)
End If
Next j
If Ubound(i_Lib.ElementNames)<0 Then bLibs.removeLibrary(i_LibName)
REM This doesn't completely work as expected:
REM The emty library will no longer be shown and counted by the container bLibs but...
REM ...having the castrated document saved and reloaded the empty library
REM is again shown in the Basic IDE. Didn't research.
REM Just another miracle of the kind some sceptics disresüectfully call a "bug".
End If
Next i
End Sub
Of course, I developed and tested with a real document. For demonstration I alo attach it here:
removeDocumentMacros_1.odt
So, here any library ending with the preclude string is spared, correct?
might merit a comment…
In this case the deserved comments would be
- I originally only was interested in the topic for “abstract” reasons, and never used the code I had posted above “in the wild”.
- Now I tested again, and found that the behavior of LibO has changed a bit since my original post: Depending on unknown circumstances it may be necesary to explicitly load the modules the code is working on.
- Back in time again: I assumed that anybody considering to rely on UserDefinedRoutines for such a sensitive task would thorougly study that code anyway.
- In short: Following my intentions, libraries having appended the precluding string to their names (
Boolean variable lPersist is True
) will not be processed at all. For libraries not marked this way, the respective check applies per module (usingBoolean variable mPersist
). - There isn’t a guarantee of any kind concerning the working of the code or the absence of serious errors whether they were present from the beginning, or put in effect later by changes in LibO.
Other options:
Sub remove_code()
doc = ThisComponent
libraries = doc.BasicLibraries
_remove_elements(libraries)
libraries = doc.DialogLibraries
_remove_elements(libraries)
doc.store(True)
End Sub
Sub _remove_elements(libraries)
DEFAULT = "Standard"
names = libraries.ElementNames
For i = LBound(names) To Ubound(names)
n = names(i)
l = libraries.getByName(n)
If n = DEFAULT Then
names2 = l.ElementNames
For i2 = LBound(names2) To Ubound(names2)
n2 = names2(i2)
l.removeByName(n2)
Next
Else
libraries.removeLibrary(n)
End IF
Next
End Sub
Used you with very CAUTION
I was looking for something similar to use. I don’t know why, but it seems the macro that @mauricio have coded always overthrow the LO.
.
I’m just a beginner, however I coded something that works without crash LO. I guess, at least.
.
But it only works if you know the libraries names where the macros are.
.
Follow the code
rem----------------------------------------------------------------------------------------------------
Sub RemoveMacros()
''' Define variables
Dim oDoc as Object, oLibBasic as Object, oLibDialog as Object
oDoc = ThisComponent 'Returne current doc
oLibBasic = oDoc.BasicLibraries 'Returne colection libraries from doc
oLibDialog = oDoc.DialogLibraries 'Returne colection dialogs from doc
'If Library an Dialogs exist, delete it
If oLibBasic.hasByName( "MyDialogs" ) And oLibDialog.hasByName( "MyDialogs" ) then
oDoc.BasicLibraries.removeLibrary( "MyDialogs" )
oDoc.DialogLibraries.MyDialogs.removeByName( "Dialog1" )
End If
'If Libraries exist, delete macros
If oLibBasic.hasByName( "NAME_OF_YOUR_LIBRARY" ) And oLibBasic.hasByName( "Standard" ) then
oDoc.BasicLibraries.removeLibrary( "NAME_OF_YOUR_LIBRARY" )
oDoc.BasicLibraries.removeLibrary( "Standard" )
End If
'Save
oDoc.store()
End Sub 'RemoveMacros
Please, if this answer worked for you somehow, mark the check box to help another users.