MsgBox showing all sections with respective headings

A

andreas

Below macro is showing all the sections of a document with their
respective first paragraph headings in a Message Box. It is working
fine. But ... please scroll down past the macro ...

Sub ShowSections()

Dim rnge As Range
Dim i As Long
Dim strHeadings As String
strHeadings = ""
With ActiveDocument
For i = 1 To .Sections.Count
Set rnge = .Sections(i).Range
rnge.End = rnge.End - 1

strHeadings = strHeadings & "Section " & i & ": " &
rnge.Paragraphs(1).Range.Text
Next i

End With
MsgBox strHeadings

End Sub

Now, here comes my question:
Some of these first paragraph headings feature the built-in numbering
(built-in numbered heading 1 paragraph style), like the following
example shows:

Section 1: Cover Sheet
Section 2: Table of Contents
Section 3: 1. Introduction
Section 4: 2. Relative Analysis
Section 5: 3. Conclusion
Section 6: Appendix
Section 7: Bibliography

How do I have to re-write above macro that the numbering is shown as
well? Now only the text of the headings that are positioned at the top
of each section are shown, but their numbering (1., 2., 3., etc.)
where applicable should show as well.

Help is appreciated. Thank you very much in advance.
 
J

Jean-Guy Marcil

andreas was telling us:
andreas nous racontait que :
Below macro is showing all the sections of a document with their
respective first paragraph headings in a Message Box. It is working
fine. But ... please scroll down past the macro ...

Sub ShowSections()

Dim rnge As Range
Dim i As Long
Dim strHeadings As String
strHeadings = ""
With ActiveDocument
For i = 1 To .Sections.Count
Set rnge = .Sections(i).Range
rnge.End = rnge.End - 1

strHeadings = strHeadings & "Section " & i & ": " &
rnge.Paragraphs(1).Range.Text
Next i

End With
MsgBox strHeadings

End Sub

Now, here comes my question:
Some of these first paragraph headings feature the built-in numbering
(built-in numbered heading 1 paragraph style), like the following
example shows:

Section 1: Cover Sheet
Section 2: Table of Contents
Section 3: 1. Introduction
Section 4: 2. Relative Analysis
Section 5: 3. Conclusion
Section 6: Appendix
Section 7: Bibliography

How do I have to re-write above macro that the numbering is shown as
well? Now only the text of the headings that are positioned at the top
of each section are shown, but their numbering (1., 2., 3., etc.)
where applicable should show as well.
Try something like this:

Dim rnge As Range
Dim i As Long
Dim strHeadings As String
Dim strPara As String

With ActiveDocument
For i = 1 To .Sections.Count
Set rnge = .Sections(i).Range.Paragraphs(1).Range
If rnge.ListFormat.ListString <> "" Then
strPara = rnge.ListFormat.ListString & " "
Else
strPara = ""
End If
rnge.End = rnge.End - 1
strHeadings = strHeadings & "Section " & i & ": " & strPara &
rnge.Text & vbCrLf
Next i

End With
MsgBox strHeadings



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

andreas

andreas was telling us:
andreas nous racontait que :














Try something like this:

Dim rnge As Range
Dim i As Long
Dim strHeadings As String
Dim strPara As String

With ActiveDocument
For i = 1 To .Sections.Count
Set rnge = .Sections(i).Range.Paragraphs(1).Range
If rnge.ListFormat.ListString <> "" Then
strPara = rnge.ListFormat.ListString & " "
Else
strPara = ""
End If
rnge.End = rnge.End - 1
strHeadings = strHeadings & "Section " & i & ": " & strPara &
rnge.Text & vbCrLf
Next i

End With
MsgBox strHeadings

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Jean-Guy,

thank you so much. It is running perfectly. Very good job!
 

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