MsgBox with number of paragraphs formatted with a user-definedparagraph style

A

andreas

Dear Experts:

I would like to loop thru all paragraphs and check how many paragraphs
have been formatted with a specific user-defined paragraph style (say
user-defined-style-1). A Msg Box is to show me the numbers of
paragraphs formatted with this style.

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

Graham Mayor

Something like

Dim oSection As Section
Dim oPara As Paragraph
Dim i As Long
i = 0
For Each oSection In ActiveDocument.Sections
For Each oPara In oSection.Range.Paragraphs
If oPara.Style = "user-defined-style-1" Then i = i + 1
Next oPara
Next oSection
MsgBox i


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Fumei2 via OfficeKB.com

Do you actually need the Section?

Sub CountEm()
Dim oPara As Paragraph
Dim j As Long

For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Style = "Yadda" Then
j = j + 1
End If
Next
MsgBox "There are " & j & " paragraphs using the Yadda style."
End Sub

works for me. Not sure why you explicitly set i = 0, as it should be 0
simply by declaring it. Although I certainly am in favour of explicitness.

Gerry

Graham said:
Something like

Dim oSection As Section
Dim oPara As Paragraph
Dim i As Long
i = 0
For Each oSection In ActiveDocument.Sections
For Each oPara In oSection.Range.Paragraphs
If oPara.Style = "user-defined-style-1" Then i = i + 1
Next oPara
Next oSection
MsgBox i
Dear Experts:
[quoted text clipped - 5 lines]
Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
A

andreas

Something like

Dim oSection As Section
Dim oPara As Paragraph
Dim i As Long
i = 0
For Each oSection In ActiveDocument.Sections
    For Each oPara In oSection.Range.Paragraphs
        If oPara.Style = "user-defined-style-1" Then i = i + 1
    Next oPara
Next oSection
MsgBox i

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>








- Zitierten Text anzeigen -

Dear Graham,

thank you very much for your professional help. It works just fine
although I have to admit, I am not quite sure why you are using the
section-object.

Regards, Andreas
 
A

andreas

Do you actually need the Section?

Sub CountEm()
Dim oPara As Paragraph
Dim j As Long

For Each oPara In ActiveDocument.Paragraphs
   If oPara.Range.Style = "Yadda" Then
      j = j + 1
   End If
Next
MsgBox "There are " & j & " paragraphs using the Yadda style."
End Sub

works for me.  Not sure why you explicitly set i = 0, as it should be0
simply by declaring it.  Although I certainly am in favour of explicitness.

Gerry





Graham said:
Something like
Dim oSection As Section
Dim oPara As Paragraph
Dim i As Long
i = 0
For Each oSection In ActiveDocument.Sections
   For Each oPara In oSection.Range.Paragraphs
       If oPara.Style = "user-defined-style-1" Then i = i +1
   Next oPara
Next oSection
MsgBox i
[quoted text clipped - 5 lines]
Help is much appreciated. Thank you very much in advance. Regards,
Andreas

--
Gerry

Message posted via OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201005/1- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Dear Gerry,

it is working just fine. Thank you very much for your professional
help. 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