Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Jan 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looping through styles & getting NumberFormat text

    Hi all,

    How can I loop through all the styles in a document, and retrieve the Number format text (this is the part of a Numbered List which is usually something like 1.1, a, b, c, ARTICLE etc.)?

    One solution which works is to use the Activedocument.Paragraphs(1).Range.ListFormat.List String method. However, this involves looping through each PARAGRAPH in the document. As a result, if a document only uses Heading 1 and Heading 2, it is impossible to retreive the Number format text for Headings 2-8.

    Please help!
    Attached Images Attached Images

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Looping through styles & getting NumberFormat text

    <P ID="edit" class=small>(Edited by Gary Frieder on 15-Jan-03 00:43. )</P>Chris,

    It's probably much easier to go in the opposite direction i.e. start with a list template and determine the name of the linked style.
    But here's something that might get you started:

    Have a look at the wdListNumberStyle enumerations in the ObjectBrowser in the VBE. Each member of wdListNumberStyle has a numeric constant associated with it - for example wdListNumberStyleLowerCaseRoman has the numeric constant 2 assigned to it.

    The following code will print to the Immediate Window the names of all the in use styles in the ActiveDocument, along with the numeric constant representing the list number style associated with that style (you will probably see a lot of them with '255' - which means no numbering.

    <pre>Public Sub GetListNumberStyleAssociatedWithStyle()

    Dim aStyle As Style
    Dim lngOutlineLevel As Long
    Dim strNumFormat As String
    Dim lngNumStyle As Long

    For Each aStyle In ActiveDocument.Styles
    If aStyle.InUse = True Then
    If Not aStyle.ListTemplate Is Nothing Then
    lngOutlineLevel = aStyle.ListLevelNumber
    strNumFormat = aStyle.ListTemplate.ListLevels(lngOutlineLevel).Nu mberFormat
    lngNumStyle = aStyle.ListTemplate.ListLevels(lngOutlineLevel).Nu mberStyle
    End If
    End If
    Debug.Print "Style name is: " & aStyle.NameLocal & _
    "; list number style is: " & CStr(lngNumStyle) & _
    "; and list number format is: " & strNumFormat & vbCr
    Next 'aStyle

    End Sub
    </pre>

    You could then build a function containing a long Select Case structure that would map the numeric list number format constants back to usable strings i.e.

    Case 3
    strNumFormat = "i"

    etc.

    It's a start anyway....

    [Edited: Chris: I've now edited this to include code which will give you the number format as well as the number style - these two combined should enable you to rebuild the number formatting as you would see it in the Customize dialog.]

    Gary

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Looping through styles & getting NumberFormat text

    In Word 2000, the concept of "in use" doesn't appear to have much meaning. Or maybe I've done something weird to the the configuration. Either way, the following led to a more manageable list:
    <pre> If strNumFormat <> vbNullString Then
    Debug.Print "Style name is: " & aStyle.NameLocal & _
    "; list number style is: " & CStr(lngNumStyle) & _
    "; and list number format is: " & strNumFormat & vbCr
    End If</pre>

    I still get lots of styles that aren't in use, but at least they really have a list template applied.

  4. #4
    New Lounger
    Join Date
    Jan 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Looping through styles & getting NumberFormat text

    Thanks for the replies guys.

    I'm a little confused though about how to determine whether a style is a numbered style or not a numbered style. (This is something I need to resolve). Is there any way of doing this? Gary, you mentioned that a lot of the styles will have '255' as the list number style value, however, I haven't seen any like this. Is this the way to determine if the style is a numbered style or not?

    For example, I am getting the following for Body Text:

    Style name is: Body Text; list number style is: 1; and list number format is: Article %1.

    However, I know that Body Text is not a numbered style.

    I'm pretty new to all this, so any help would be much appreciated.

    Thanks chaps!

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Looping through styles & getting NumberFormat text

    > However, I know that Body Text is not a numbered style.

    Post that document and let's see. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Looping through styles & getting NumberFormat text

    Jefferson,

    First off, thanks for your code emendation - that is a good idea.
    However it still seems that loads and loads of styles which I don't really expect to have a number format, are still showing up in the list in the Immediate Window - along the lines of what Chris is reporting.

    (Although by the way this issue makes a great argument for those who recommend using one's own custom-named styles rather than the built-in styles - if you could confidently filter out all styles here where BuiltIn = False, you would end up with a much more manageable list!)

    Anyway it seems like there is more to this than I thought on the first go-round:
    - Why would a style like "Plain Text" be showing a list number format of "%1"?
    - In Word's Style dialog, it does state whether the style is Bulleted, Numbered, or Outline Numbered; it's still unclear where Word is getting that information from.

    Will try to play around with this some more if I can, and post back...

    Gary

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Looping through styles & getting NumberFormat text

    Jefferson, Chris,

    Here is a more pragmatic code solution, which also appears to have the advantage of actually working! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    <pre>Public Sub GetNumberingTypeForEachStyle()

    Dim aStyle As Style
    Dim strStyDesc As String
    Dim strNumberingType As String

    For Each aStyle In ActiveDocument.Styles
    If aStyle.InUse = True Then
    strStyDesc = aStyle.Description
    If InStr(strStyDesc, "Bulleted") Then
    strNumberingType = "Bulleted"
    ElseIf InStr(strStyDesc, "Numbered") Then
    strNumberingType = "Numbered"
    ElseIf InStr(strStyDesc, "Outline numbered") Then
    strNumberingType = "Outline numbered"
    Else
    strNumberingType = "Not numbered"
    End If
    End If
    Debug.Print "Style name is: " & aStyle.NameLocal & _
    "; the numbering type is: "; strNumberingType
    Next 'aStyle

    End Sub
    </pre>

    Gary

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Looping through styles & getting NumberFormat text

    Gary,

    Very nice, but I suspect this will only work for people using English menus.

    StuartR

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Looping through styles & getting NumberFormat text

    Stuart,

    Good point; guess my anglocentrism is showing.

    I've never had occasion to determine the current Office language setting, but from the Help it appears that:
    <pre>Application.LanguageSettings.LanguageID(msoLa nguageIDUI)</pre>

    should return the language ID for the current Office user interface language.
    If that's the case, then the following should work:

    <pre>Option Explicit

    Dim strBulleted As String
    Dim strNumbered As String
    Dim strOutlineNumbered As String
    Dim strNotNumbered As String

    Public Sub GetNumberingTypeForEachStyle()

    'Gary Frieder Woody's Lounge 2003
    Dim aStyle As Style
    Dim strStyDesc As String
    Dim strNumberingType As String

    If Not fGetTranslations(lngGetAppLangID) Then Exit Sub

    For Each aStyle In ActiveDocument.Styles
    If aStyle.InUse = True Then
    strStyDesc = aStyle.Description
    If InStr(strStyDesc, strBulleted) Then
    strNumberingType = strBulleted
    ElseIf InStr(strStyDesc, strNumbered) Then
    strNumberingType = strNumbered
    ElseIf InStr(strStyDesc, strOutlineNumbered) Then
    strNumberingType = strOutlineNumbered
    Else
    strNumberingType = strNotNumbered
    End If
    End If
    Debug.Print "Style name is: " & aStyle.NameLocal & _
    "; the numbering type is: "; strNumberingType
    Next 'aStyle

    End Sub

    Public Function lngGetAppLangID() As Long

    lngGetAppLangID = Application.LanguageSettings.LanguageID(msoLanguag eIDUI)

    End Function

    Public Function fGetTranslations(LangID As Long) As Boolean

    Select Case LangID
    Case 1033
    strBulleted = "Bulleted"
    strNumbered = "Numbered"
    strOutlineNumbered = "Outline numbered"
    strNotNumbered = "Not numbered"
    Case 1034 'Spanish
    strBulleted = "SpanishForBulleted"
    strNumbered = "SpanishForNumbered"
    strOutlineNumbered = "SpanishForOutline numbered"
    strNotNumbered = "SpanishForNot numbered"
    Case Else ' default to English
    strBulleted = "Bulleted"
    strNumbered = "Numbered"
    strOutlineNumbered = "Outline numbered"
    strNotNumbered = "Not numbered"
    End Select

    fGetTranslations = True

    End Function
    </pre>

    You would have to add Case statements for each language that you are likely to need to deal with; and hard code in the translations for the needed terms.

    Gary

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Looping through styles & getting NumberFormat

    > "in use" doesn't appear to have much meaning.

    In Word97/SR2 a style "in use" seems to indicate that is was once, however fleetingly, in use.

    Using a style once on a single paragraph, then applying a different style to that paragraph leaves the first style flagged as "in use".


    My solution to determining which styles are truly "in use" is to use one of two loops:

    1) loop through each paragraph and tabulate unique style names

    2) loop through all styles "in use" and Edit.Find a paragraph with that style.

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Looping through styles & getting NumberFormat

    Chris,

    In case it's any 'use' to anyone, some code that works along the lines of your '2)' can be found <!post=here,58830>here<!/post>.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •