Consulting

Results 1 to 9 of 9

Thread: Solved: Test for numbered paragraphs

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Solved: Test for numbered paragraphs

    I've imported a document with a mixture of numbered and unnumbered paragraphs and wish to apply new styles to both. What is the code which would detect this.
    Something like
    [VBA] If selection.numbered = true then
    Style1
    else
    Style2
    End if
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Well hello there...

    I don't think there is a property that directly returns whether a paragraph is a list item or not, I think the easiest way would be to spin through the ListParagraphs collection applying your first style, then spin through the paragraphs collection and apply the second style to anything that isn't already the first style. Not the last word in efficiency but only a few lines of code[VBA]Dim p As Paragraph

    For Each p In ActiveDocument.ListParagraphs
    p.Style = "MyListStyle"
    Next

    For Each p In ActiveDocument.Paragraphs
    If p.Style <> "MyListStyle" Then p.Style = "MyOtherStyle"
    Next[/VBA]
    K :-)

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Killian,
    ListParagraphs is the propery I was needing.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Actually, there IS a property that directly returns whether the paragraph is a list item. It is ListFormat.ListType.
    [vba]Sub mPara()
    Dim r As Word.Range
    Dim oPara As Word.Paragraph

    For Each oPara In ActiveDocument.Paragraphs()
    Set r = oPara.Range
    Select Case r.ListFormat.ListType
    Case wdListNoNumbering
    MsgBox "No numbering" & vbCrLf & _
    r.Text
    Case wdListBullet
    MsgBox "Bulleted list" & vbCrLf & _
    r.Text
    Case wdListListNumOnly
    MsgBox "ListNum only" & vbCrLf & _
    r.Text
    Case wdListMixedNumbering
    MsgBox "Mixed list" & vbCrLf & _
    r.Text
    Case wdListSimpleNumbering
    MsgBox "Simple numbering" & vbCrLf & _
    r.Text
    Case wdListOutlineNumbering
    MsgBox "Outline Numbering"
    End Select
    Set r = Nothing
    Next
    End Sub[/vba]

    Try running this with a bunch of sample paragraphs, including one NOT listed. You will find some odd (if not bizarre) returns. Particualrly numbering. Bullets are fine.

    For example, if you have a paragraph and click the Numbering icon...what do you think you get? Check it out.

    Further, say you make the next paragraph also numerbred? Just press enter to do so. BUT, click the right indent button to indent it. Would you think the ListType would change. It does, but in, I think, a strange way.

    Anyway, the point being is you can, with some of the head shaking on the Word Object Model, directly extract if a paragraph is listed, or not.

    NOTE! ListType is a property of the Range object - if will not work with Selection.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Malcom & the gang!,

    The code provided for looping the para collection is obviously great!

    But....

    You could save so much time in a large document if you just use a Find and replace sub that wil find and replace a particular style with another one in one execute for the whole document. (Provided you are using styles)

    If you need code I'll give it.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Gerry,
    I'll look into your code more closely, but for the moment, the following works with my current data. It sequentially numbers the already numbered paragraphs, but adds the indenting and other format changes that I'm after.
    [VBA]
    Sub listing()
    For Each para In ActiveDocument.ListParagraphs
    para.Style = ActiveDocument.Styles("BQItem")
    Next para
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Joost,
    I'm trying to import an Unstyled document into one with styles, and this code is to apply the most common styles. It takes a few seconds to run, but I can live with that.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Malcom,

    Yepz indeed your approach is just fine for dealing with unstyled documents. My find and replace sub would perhaps not work as expected...

    Better to examine para's..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  9. #9
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Anyway, the point being is you can, with some of the head shaking on the Word Object Model, directly extract if a paragraph is listed, or not.
    Good call Gerry! That's a useful bit of info when more control is required between ListTypes (it's going straight in my Word code library for future reference)
    As it turns out, for converting an unstyled documents, spinning through the paragraph collections doesn't seem so bad
    K :-)

Posting Permissions

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