Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to list used styles in a wordDocument

Status
Not open for further replies.

nesplb

Programmer
Jul 29, 2003
109
NO
Hi!
I want to list all the used styles in a document. How can I do this?
I know that ActiveDocument.styles will give all the styles in the template, but I just want to list the ones that I've used....

In advance thanks!

Pål Nesteby

PDC-Tangen
Norway
 
There are a number of ways. here's one:

Code:
Dim myPara As Paragraph
    Dim myStory As Range
    Dim myCollection As Collection
    
    Set myCollection = New Collection
    
    For Each myStory In ActiveDocument.StoryRanges
        For Each myPara In myStory.Paragraphs
            On Error Resume Next
                myCollection.Add myPara.Style.NameLocal
            On Error GoTo 0
        Next
    Next
    [COLOR=green]' At this point myCollection contains list of styles in use by document
[/color]
 
Here's another one:
Code:
Sub liststyles()
Dim sty As Style
For Each sty In ActiveDocument.Styles
    Selection.TypeText sty.NameLocal & vbCrLf
Next
End Sub

[blue]The last voice we will hear before the world explodes will be that of an expert saying:
"This is technically impossible!" - Sir Peter Ustinov[/blue]
andreas.galambos@bowneglobal.de
HP:
 
Hi nesplb,

Well, strongm's method picks up all paragraph styles, but not character styles, and not quite in all the document (it misses things like even page headers); MakeItSo's method you already know about and it gives a massive superset.

From Help, we get ..

Code:
[blue]Set mydoc = ActiveDocument
msg = "Styles in use:" & vbCr
For Each sty In mydoc.Styles
    If sty.InUse = True Then
        With mydoc.Content.find
            .ClearFormatting
            .Text = ""
            .Style = sty
            .Execute Format:=True
            If .Found = True Then
               msg = msg & sty & vbCr
            End If
        End With
    End If
Next sty
MsgBox msg[/blue]
which (a) only works on the main story, and
(b) doesn't work if the story contains only a single paragraph and no character styles - I don't know why; it's something to do with the way Find works.

Combining strongm and Help (with a little correction) is what we need; doing it is another matter! At the moment I can't get it to work - I will keep trying and post back, unless somebody smarter posts sooner.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Wieird - my code picks up styles in even page headers on my system. I wonder why it fails on yours, Tony. However, as yu say, it sure as heck doesn't pick up character styles...now, let's see...
 
Hi strongm,

My apologies. It does pick up even page headers; what it doesn't pick up is headers and footers in multiple Sections. I didn't try it out - I know why; I just said the wrong thing. [smile]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thank you very much to both of you!:)
-But MakeITSo: Wouldn't your example list all styles in available templates, not just styles I've used?


Pål Nesteby

PDC-Tangen
Norway
 
Again....:)
ThanX to all of you!
I appreciate all your help!!:)



Pål Nesteby

PDC-Tangen
Norway
 
Ok, exacty same principle as my earlier solution, except we step through character by character instead of para by para
[colour blue]
Code:
Dim myStory As Range
    Dim myCollection As Collection
    Dim myCharInfo As Range
    
    Set myCollection = New Collection
    
    For Each myStory In ActiveDocument.StoryRanges
        For Each myCharInfo In myStory.Characters
            On Error Resume Next
                myCollection.Add myCharInfo.Style.NameLocal, myCharInfo.Style.NameLocal
            On Error GoTo 0
        Next
    Next
    [COLOR=green]' At this point myCollection contains list of styles in use by document
[/color]
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top