A
andreas
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
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