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]
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'
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 :-)
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'
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.
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)
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'
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'
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)
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)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.
As it turns out, for converting an unstyled documents, spinning through the paragraph collections doesn't seem so bad
K :-)