Dear Experts:
I am trying to write a macro that shows me all the headings of all the
sections in a MessageBox, i.e the text from the first paragraph of
each section along with the section number should appear in a Message
Box:
Example:
Section 1: Cover Sheet
Section 2: Acknowledgments
Seciont 3: Contents
Section 4: Preface
Section 5: Chapter 1
Section 6: Chapter 2
etc.
I was able to write part of the macro, ie. loop through all the
sections and get the text from the first paragraph of each section.
But there it ends. Could anybody please give me a hand. Thank you very
much in advance.
Sub ShowSectionHeadings()
Dim rng As Range
Dim sect As Section
Dim strSectionHeading As String
For Each sect In ActiveDocument.Sections
'Get text from first paragraph in section
strSectionHeading = sect.Range.Paragraphs(1).Range.Text
'Trim paragraph mark if present
If Right(strSectionHeading, 1) = vbCr Then
strSectionHeading = Left(strSectionHeading, Len(strSectionHeading)
- 1)
End If
Next sect
End Sub
Doug Robbins - Word MVP - 10 Feb 2007 15:07 GMT
Try
Dim rnge As Range
Dim i As Long
Dim strHeadings As String
strHeadings = ""
With ActiveDocument
For i = 1 To .Sections.Count - 1
Set rnge = .Sections(i).Range
rnge.End = rnge.End - 1
strHeadings = "Section " & i & ": " & strHeadings & rnge.Text & vbCr
Next i
Set rnge = .Sections(Last).Range
strHeadings = "Section " & i + 1 & ": " & strHeadings & rnge.Text
End With
MsgBox strHeadings

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> Dear Experts:
>
[quoted text clipped - 38 lines]
>
> End Sub