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.

Re: MsgBox showing all sections with respective headings by Jean-Guy

Jean-Guy
Mon Feb 12 20:14:24 CST 2007

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: MsgBox showing all sections with respective headings by andreas

andreas
Tue Feb 13 02:23:01 CST 2007

On 13 Feb., 03:14, "Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote:
> 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
> jmarcilREM...@CAPSsympatico.caTHISTOO
> 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!