Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

MsBox showing all the headings of each section

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andreas - 10 Feb 2007 14:40 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.