PDA

View Full Version : Solved: Test for numbered paragraphs



mdmackillop
06-23-2005, 04:46 AM
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
If selection.numbered = true then
Style1
else
Style2
End if

Killian
06-23-2005, 05:46 AM
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 codeDim 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

mdmackillop
06-23-2005, 06:04 AM
Thanks Killian,
ListParagraphs is the propery I was needing.
Regards
MD

fumei
06-23-2005, 10:12 AM
Actually, there IS a property that directly returns whether the paragraph is a list item. It is ListFormat.ListType.
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

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.

MOS MASTER
06-23-2005, 11:33 AM
Hi Malcom & the gang!, :yes

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.:whistle:

mdmackillop
06-23-2005, 11:42 AM
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.

Sub listing()
For Each para In ActiveDocument.ListParagraphs
para.Style = ActiveDocument.Styles("BQItem")
Next para
End Sub

mdmackillop
06-23-2005, 11:45 AM
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.

MOS MASTER
06-23-2005, 11:47 AM
Hi Malcom, :yes

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..:whistle:

Killian
06-24-2005, 04:37 AM
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) :thumb
As it turns out, for converting an unstyled documents, spinning through the paragraph collections doesn't seem so bad