Applying a user-defined paragraph style to numbered lists

A

andreas

Dear Experts:

I got a document where several paragraphs have got numbered lists.
These numbered lists have been created by selecting "Format-Bullets
and Numbering".

I now would like a macro to find all those instances of numbered lists
and apply a user-defined paragraph style to them . Is this possible?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
C

Cindy M.

Hi Andreas,
I got a document where several paragraphs have got numbered lists.
These numbered lists have been created by selecting "Format-Bullets
and Numbering".

I now would like a macro to find all those instances of numbered lists
and apply a user-defined paragraph style to them . Is this possible?

It can be done quite easily (see below). But the question is: what kind
of formatting is it you want to change? Applying a paragraph style to a
list that is not linked to any numbering will remove the numbering.

And if you're trying to affect indents, keep in mind these should be
handled by the numbering style, not the paragraph style.

Sub ApplyParaStyleToLists()
Dim doc As Word.Document
Dim para As Word.Paragraph

Set doc = ActiveDocument
For Each para In doc.Content.ListParagraphs
para.Range.Style = "Test"
Next
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
P

Pesach Shelnitz

Hi Andreas,

I wasn't sure from your question whether the new style would have numbering
or not, so I added safeguards to the following macro so that it could be used
in either case. Try it and see if it does what you need.

Sub ChangeListStyles()
Const Error_StyleNotFound = 5941
Dim docRange As Range
Dim parRange As Range
Dim styleName As String
Dim myStyle As Variant

styleName = InputBox("Type the style name.", "Style Name", , 1000, 100)
On Error Resume Next
Set myStyle = ActiveDocument.Styles(styleName)
If Err.Number = Error_StyleNotFound Then
MsgBox "The style specified was not found."
Exit Sub
End If
On Error GoTo 0
Set docRange = ActiveDocument.Range
With docRange
Do While .ListParagraphs.Count > 0
Set parRange = .ListParagraphs(1).Range
.ListParagraphs(1).Reset
parRange.Paragraphs(1).style = styleName
If parRange.End < .End Then
.Start = parRange.End + 1
Else
Exit Do
End If
Loop
End With
docRange.Collapse Direction:=wdCollapseEnd
parRange.Collapse Direction:=wdCollapseEnd
Set docRange = Nothing
Set parRange = Nothing
End Sub
 
A

andreas

Hi Andreas,



It can be done quite easily (see below). But the question is: what kind
of formatting is it you want to change? Applying a paragraph style to a
list that is not linked to any numbering will remove the numbering.

And if you're trying to affect indents, keep in mind these should be
handled by the numbering style, not the paragraph style.

Sub ApplyParaStyleToLists()
    Dim doc As Word.Document
    Dim para As Word.Paragraph

    Set doc = ActiveDocument
    For Each para In doc.Content.ListParagraphs
        para.Range.Style = "Test"
    Next
End Sub

Cindy Meister
INTER-Solutions, Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17 2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)

Dear cindy,

ok, thank you very much for your swift help. But what if I want to
apply this user defined style just to numbered lists. Your macro also
applies to bulleted lists. The latter ones should not be changed.

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
K

Klaus Linke

Hi Andreas,

Sub ApplyParaStyleToLists()
Dim doc As Word.Document
Dim para As Word.Paragraph

Set doc = ActiveDocument
For Each para In doc.Content.ListParagraphs
Select Case para.Range.ListFormat.ListType
Case wdListOutlineNumbering, wdListSimpleNumbering
para.Range.Style = "Test"
Case wdListMixedNumbering
' ???
End Select
Next

End Sub

Internally, there isn't much difference between numbering and bullets, and
both can be mixed in the same list template. In complicated cases, you might
have to look at the ListFormat.ListString property to determine what to do.

Regards,
Klaus


Hi Andreas,



It can be done quite easily (see below). But the question is: what kind
of formatting is it you want to change? Applying a paragraph style to a
list that is not linked to any numbering will remove the numbering.

And if you're trying to affect indents, keep in mind these should be
handled by the numbering style, not the paragraph style.

Sub ApplyParaStyleToLists()
Dim doc As Word.Document
Dim para As Word.Paragraph

Set doc = ActiveDocument
For Each para In doc.Content.ListParagraphs
para.Range.Style = "Test"
Next
End Sub

Cindy Meister
INTER-Solutions,
Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17
2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)

Dear cindy,

ok, thank you very much for your swift help. But what if I want to
apply this user defined style just to numbered lists. Your macro also
applies to bulleted lists. The latter ones should not be changed.

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
P

Pesach Shelnitz

Hi Andreas,

After seeing Cindy's and Klaus's approach, I can greatly simplify my macro
to give the result that you want and still leave the code for getting the
name of the style from the user.

Sub ChangeListStyles()
Const Error_StyleNotFound = 5941
Dim para As Paragraph
Dim styleName As String
Dim myStyle As Variant

styleName = InputBox("Type the style name.", "Style Name", , 1000, 100)
On Error Resume Next
Set myStyle = ActiveDocument.Styles(styleName)
If Err.Number = Error_StyleNotFound Then
MsgBox "The style specified was not found."
Exit Sub
End If
On Error GoTo 0
With ActiveDocument
For Each para In .ListParagraphs
Select Case para.Range.ListFormat.ListType
Case wdListOutlineNumbering, wdListSimpleNumbering
para.style = styleName
Case wdListMixedNumbering
' ???
End Select
Next
End With
End Sub


Pesach
 
A

andreas

Hi Andreas,

Sub ApplyParaStyleToLists()
 Dim doc As Word.Document
 Dim para As Word.Paragraph

 Set doc = ActiveDocument
 For Each para In doc.Content.ListParagraphs
  Select Case para.Range.ListFormat.ListType
    Case wdListOutlineNumbering, wdListSimpleNumbering
      para.Range.Style = "Test"
    Case wdListMixedNumbering
      ' ???
  End Select
 Next

End Sub

Internally, there isn't much difference between numbering and bullets, and
both can be mixed in the same list template. In complicated cases, you might
have to look at the ListFormat.ListString property to determine what to do.

Regards,
Klaus









Dear cindy,

ok, thank you very much for your swift help. But what if I want to
apply this user defined style just to numbered lists. Your macro also
applies to bulleted lists. The latter ones should not be changed.

Help  is much appreciated. Thank you very much in advance. Regards,
Andreas- Hide quoted text -

- Show quoted text -

Hi Klaus,

great help. it works. Thank you very much. Regards, Andreas
 
A

andreas

Hi Andreas,

After seeing Cindy's and Klaus's approach, I can greatly simplify my macro
to give the result that you want and still leave the code for getting the
name of the style from the user.

Sub ChangeListStyles()
    Const Error_StyleNotFound = 5941
    Dim para As Paragraph
    Dim styleName As String
    Dim myStyle As Variant

    styleName = InputBox("Type the style name.", "Style Name", , 1000, 100)
    On Error Resume Next
    Set myStyle = ActiveDocument.Styles(styleName)
    If Err.Number = Error_StyleNotFound Then
         MsgBox "The style specified was not found."
         Exit Sub
    End If
    On Error GoTo 0
    With ActiveDocument
        For Each para In .ListParagraphs
            Select Case para.Range.ListFormat.ListType
                Case wdListOutlineNumbering, wdListSimpleNumbering
                    para.style = styleName
                Case wdListMixedNumbering
                   ' ???
            End Select
        Next
    End With
 End Sub

Pesach

Hi Pesach,

terrific help! Thank you very much for your professional coding.
Regards, Andreas
 
A

andreas

Hi Andreas,

After seeing Cindy's and Klaus's approach, I can greatly simplify my macro
to give the result that you want and still leave the code for getting the
name of the style from the user.

Sub ChangeListStyles()
    Const Error_StyleNotFound = 5941
    Dim para As Paragraph
    Dim styleName As String
    Dim myStyle As Variant

    styleName = InputBox("Type the style name.", "Style Name", , 1000, 100)
    On Error Resume Next
    Set myStyle = ActiveDocument.Styles(styleName)
    If Err.Number = Error_StyleNotFound Then
         MsgBox "The style specified was not found."
         Exit Sub
    End If
    On Error GoTo 0
    With ActiveDocument
        For Each para In .ListParagraphs
            Select Case para.Range.ListFormat.ListType
                Case wdListOutlineNumbering, wdListSimpleNumbering
                    para.style = styleName
                Case wdListMixedNumbering
                   ' ???
            End Select
        Next
    End With
 End Sub

Pesach

Hi Pesach,

further to your and Klaus Linke's code it just came to my mind to
expand this code a little bit, if possible.
Is it possible to show the number of application of this user-defined
style to existing numbered lists or if no instances of numbered lists
are found, a msgBox is to tell so.

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
K

Klaus Linke

Hi Andreas,

I'm afraid Pesach has spoiled you by posting elborate macros to your
questions <g, d & r>

If you want to know the number of times some code is run, you define a
counter:

Dim i as Long

and increase it every time the code is called:

Case wdListOutlineNumbering, wdListSimpleNumbering
para.style = styleName
i=i+1
Case wdListMixedNumbering
...

Then at the end, you can do some output:
MsgBox "The style " & stylename _
& " was applied " & Trim(Str(i)) & " times.", _
vbInformation, "Finished applying " & stylename

Regards,
Klaus
 
A

andreas

Hi Andreas,

I'm afraid Pesach has spoiled you by posting elborate macros to your
questions <g, d & r>

If you want to know the number of times some code is run, you define a
counter:

Dim i as Long

and increase it every time the code is called:

  Case wdListOutlineNumbering, wdListSimpleNumbering
     para.style = styleName
     i=i+1
  Case wdListMixedNumbering
     ...

Then at the end, you can do some output:
  MsgBox "The style " & stylename _
    & " was applied " & Trim(Str(i)) & " times.", _
    vbInformation, "Finished applying " & stylename

Regards,
Klaus







- Show quoted text -


Hi Klaus,

it is not only Pesach spoiling me with elaborate codes. All of the
experts incl. you provide terrific and extensive help.

It is working as desired.

Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top