[Solved] Error trying to run Villeroy's "RemoveUnusedStyles"

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
foxcole
Volunteer
Posts: 1507
Joined: Mon Oct 08, 2007 1:31 am
Location: Minneapolis, Minnesota

[Solved] Error trying to run Villeroy's "RemoveUnusedStyles"

Post by foxcole »

I pasted Villeroy's three sections of code from the first page of this thread into OpenOffice.org Basic> My Macros> Standard> Module1... his instruction was to "copy everything into one module" so I assumed it was okay to put everything in there, just pasted one after the other.

(But it bothers me a little that there are three pieces of code, not just one macro, and two of the sections were prefaced with "Part of my module Awt.dynDialogs:" which doesn't look like a Basic module to me even though the first comment in the code is a line identifying it as BASIC; and with "Part of my module Standard.myTools" which is followed by something that distinctly does not look like BASIC, so I wonder if I did the wrong thing there.)

I saved the document and then tried to run RemoveUnusedStyles but got an error:

"BASIC runtime error:
Argument is not optional."

This line of code in the macro is highlighted:
sUsed() = getStyleNames(oFamily,bLocalized:=False,bUsed:=False,bUserDef:=true)

This is the line preceding it:
Dim sUsed() as String, sMsg$,iAnswer%

Not that I'm an expert by any means, but the sub looks like a standard Basic macro to me... so I don't understand why an error might occur, especially since I know other people have run this successfully in the same OOo version (except mine's portable, where others are running OOo locally), if not the same OS, as mine. Should I have copied the three pieces of code into three different libraries? (I've never looked at the Python or BeanShell ones before, barely noticed them... BeanShell? What's BeanShell?)

I would of course greatly appreciate any assistance!
Last edited by foxcole on Sat Feb 02, 2008 11:27 pm, edited 1 time in total.
Cheers!
---Fox

OOo 3.2.0 Portable, Windows 7 Home Premium 64-bit
User avatar
Villeroy
Volunteer
Posts: 31264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Error rrying to run Villeroy's "RemoveUnusedStyles"

Post by Villeroy »

I try my best to write reusable code in small chunks of code.
Sub RemoveUnusedStyles(oFamily,bAsk as Boolean) does what it's name suggest if you provide any document's style family (char, paragraph, bullets, cell styles) and another boolean argument if you want to be asked befor deletion. So you can call this generic routine from your own more sprecialized routines without rewriting everything.
The most simple use case would be a routine like this:

Code: Select all

Sub AskForRemovingUnusedCharStyles()
   oFamily = thisComponent.StyleFamilies.getByName("CharStyles")
   RemoveUnusedStyles(oFamily, bAsk:=True)
End Sub
The routine does not take any arguments, so you can call it directly from a button, a hyperlink, or Tools>Macros>... It calls my routine passing over a style family (current document's char styles) and a "True" value for argument "bAsk".
My routine test_RemoveUnusedStyles() does not take arguments neither. It calls other routines to create a simple dialog box which is supposed to ask for the specific style family from where you wish to remove the unused ones. Again, the other routines are written in a more generic manner. You can reuse them to let the user choose on of many options in arbitrary context.

Code: Select all

Sub test_ListboxChoice()
oDlg = getListboxDialog("TITLE", "Pick a name", Array("John", "foxcole", "David", "Villeroy")) 
If oDlg.execute = 0 then
  Msgbox "You aborted the choice"
else
  Msgbox "You picked "& oDlg.getControl("ListBox").getSelectedItem
endif
End Sub
The short answer is: Run the routine that does not require additional arguments. In this case test_RemoveUnusedStyles(). It is just one example for how one could use the other stuff.

Oh, while we are in this matter:
Like 95% of all macros, this one is dangerous if you do not understand exactly all it's implications. I used this one with cell styles of spreadsheet documents and all conditional formatting stopped working. Why? The collection of used styles does not include the conditional "would-be-used-styles". An improved version would scan the entire document for conditional formattings and add all conditional cell styles to the list of used styles. I'm not aware of the implications of conditional styles in Writer.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
foxcole
Volunteer
Posts: 1507
Joined: Mon Oct 08, 2007 1:31 am
Location: Minneapolis, Minnesota

Re: Error rrying to run Villeroy's "RemoveUnusedStyles"

Post by foxcole »

Thank you!!! That helps considerably.
Interesting point about conditional styles. I don't use them much, but it will be interesting to play around a bit and see what happens....
Cheers!
---Fox

OOo 3.2.0 Portable, Windows 7 Home Premium 64-bit
User avatar
Villeroy
Volunteer
Posts: 31264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Error rrying to run Villeroy's "RemoveUnusedStyles"

Post by Villeroy »

Just found an error that occurs if you paste all 3 chunks of code into one module:

Code: Select all

Option Explicit
Remove both occurrences of that statement.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
foxcole
Volunteer
Posts: 1507
Joined: Mon Oct 08, 2007 1:31 am
Location: Minneapolis, Minnesota

Re: Error rrying to run Villeroy's "RemoveUnusedStyles"

Post by foxcole »

That's it!! I was going to post again with a different error (and a problem that the first built-in style of groups, like Heading 1, List 1, etc. had been deleted) but now it works flawlessly when I run test_RemoveUnusedStyles. Thanks, Villeroy!! Very nice work!
Cheers!
---Fox

OOo 3.2.0 Portable, Windows 7 Home Premium 64-bit
Post Reply