List or delete [certain] unused styles

In the styles pane I can see the paragraph styles filtered in various ways (All, Custom, Outline, etc.). I wish that “Unused styles” would appear in that list so I could right-click them and delete them without having the list jump to the top (usually at a style that is in use) each time.

Is there a macro to delete styles by user-supplied name, preferably with wildcards?

My problem is, I have some old docs from MS Word, and I’ve used my LO custom styles in them so there are a bunch of similarly-named MS styles that came over but became unused. A convenient way to delete those would sure be convenient. :slight_smile:

Neither for a text document nor for a specific style I know a ReferenceCounter or a Boolean property like IsUsed concerning styles. (.IsPhysical=False seemingly doesn’t apply to user styles.) This looks strange to me since there references might be counted internally: Try to delete a used style from the S&F panel, and you will see what I take as an indication. But a recorded macro for the purpose will again not show the alert.
(This despite the fact that reference counting is a general concept of programming.)

Thus: The only way I know to avoid the deletion of a used styles by a macro is to count the references (cases-of-usage) explicitly. Since ParaStyle also is used in TextFrame and TextTable (In tables per cell! Are there more classes?) there can be lots of objects needing to be tested for a specific ParaStyle property this requires some patience during programming and probably also when the code is running for a large document. (For character styles it can be much worse.) In case of deeper interest read OOME Chapter 13.12. by Andrew Pitonyak. (I don’t think there were changed relevant thing so far since.)

To simply delete a paragraph style without any checks you can use this code:

Sub deleteParaStyle(Optional pParaStyleName As String) REM Optuonal only for the demo!
REM There are no checks implemented! You can analyse the parameter, of course.
REM Paragraphs set to a deleted style fall back to its parent style.
REM === Only for the demo again
If IsMissing(pParaStyleName) Then pParaStyleName = "psVariant1"
REM === End===  Comes actually working part.

doc0              = ThisComponent
styleFamilies     = doc0.StyleFamilies
paraStyles        = styleFamilies.getByName("ParagraphStyles")
REM isonParaStyle = paraStyles.GetByName(pParaStyleName) REM Only for inspection.
End Sub

Many thanks- with apologies for taking so long to say. Your code is much appreciated. I really have no clue about the LO object model, so pointers are very helpful!