MsgBox: First heading level 1 is on section x, the last heading level 1 is on section y ?

A

andreas

Hello,

I got a long word document with a lot of sections.

Section 1: Title Page
Section 2: Acknowledgments
Section 3: Abstract
Section 4: Table of Contents
Section 5: Table of Figures
Section 6: List of Tables

Sections 7-13: Main document with built-in heading styles (level 1 to
level 4)

Section 14: Appendix


Part of a macro I wrote should show the user right at the beginning in
a msgbox or inputbox that:

-The section with the first heading level 1 (i.e. Chapter 1) is on
section x.
-The section with the last heading level 1 (i.e. Chapter 13) is on
section y.

Can this be achieved by using VBA commands?I tried things like
Selection.GoTo What:=wdGoToHeading etc. But I cannot tell Word to jump
to the first or the last of the heading 1 levels, can I?

Help is appreciated. Thank you in advance.

Regards,

Andreas
 
L

Lene Fredborg

You can, for example, use Find to determine where the first and the last
Heading 1 are found. This can be done by searching forward from the start of
the document to find the first Heading 1 and searching backward from the end
of the document to find the last Heading 1.

Below I have included a macro that performs the Find and shows a dialog box
that lets the user go to the first Heading 1. Change the “action†part to fit
your needs. You may also need to add further checks and error handling.


Sub FindFirstOrLastHeading1()
Dim oRange_FirstHeading1 As Range
Dim oRange_LastHeading1 As Range
Dim nSection_FirstHeading1 As Long
Dim nSection_LastHeading1 As Long
Dim Msg As String

'To find first Heading 1, search from start of document
Selection.HomeKey (wdStory)
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles(wdStyleHeading1)
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
'Search forward
.Forward = True
'Find only one occurrence
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
'Find the heading
.Execute
'Make it possible to refind
Set oRange_FirstHeading1 = Selection.Range
'Determine the section number
nSection_FirstHeading1 = Selection.Sections(1).Index
End With

'To find last Heading 1, search from end of document
'Most Find settings are still OK
Selection.EndKey (wdStory)
With Selection.Find
'Search backward
.Forward = False
.Execute
'Make it possible to refind
Set oRange_LastHeading1 = Selection.Range
'Determine the section number
nSection_LastHeading1 = Selection.Sections(1).Index
End With

'Clear Find setting
Selection.Find.ClearFormatting

'----------
Selection.HomeKey (wdStory)
'Add code that performs the desired actions, e.g.
Msg = "The first chapter is found in Section " & nSection_FirstHeading1
& "." & vbCr & _
"The last chapter is found in Section " & nSection_LastHeading1 &
"." & vbCr & vbCr & _
"Do you want to go to the first chapter now?"
'Go to first chapter if user selects yes
'Else do nothing
If MsgBox(Msg, vbYesNo, "Chapter Info") = vbYes Then
oRange_FirstHeading1.Select
End If
'----------

Set oRange_FirstHeading1 = Nothing
Set oRange_LastHeading1 = Nothing
End Sub

--
Regards
Lene Fredborg
DocTools – Denmark
www.thedoctools.com
Document automation – add-ins, macros and templates for Microsoft Word
 
A

andreas

Dear Lene,

thanks a lot for your valuable help. It is running just fine with the
exception of the first "Find code", ie. the part which is searching for
the first heading 1 level.

If I run this part separately, i.e. searching for the first heading 1
level (I took the macro snippet and created a separate macro), the
screen does not jump to the first heading 1 level and selects it but to
the section break before (no selecting) and therefore the section
number is not the right one. In my specific case it should be section 7
and the msgbox is telling me that the first heading level is on section
6.

Strange enough, the section of the last heading level 1 is returned
correctly.
I tried it on other sample documents with the same result.

I got a simple workaround. I just add "+1" to the
"nSection_FirstHeading1" part of the msgbox function. Please see below

Msg = "The first chapter is found in Section " & nSection_FirstHeading1
+ 1 & "." & vbCr & _
"The last chapter is found in Section " & nSection_LastHeading1
& "." & vbCr & vbCr &

Keeping in mind that I have to add +1 to the section number for the
first heading 1 level found, everything is fine for me. Thank you again
for your great advice.

P.S.: When I tried to write this macro myself before seeking help, I
encountered exactly the same problem and thought that I did not
programm correctly. I guess it is a Microsoft VBA Bug. What do you
think?

Regards and my best wishes for 2007,

Andreas
 
A

andreas

Lene,

Thanks for the quick answer. I thought I had already considered what
you tried to tell me, but apparently I overlooked a little detail. Read
my explanation of the error occurred:

I double-checked and found something out that I was not aware of
before.
If I just select the paragraph mark that is positioned left of the
double dotted line of the section break and apply, say the "normal
style" to it, then the paragraph mark will feature exactly this style,
but If I move the cursor one character to the right of that paragraph
mark (now the cursor is blinking right before the double dotted line)
the style changes to heading 1. And here lies the error. I now selected
the whole section break (instead of only the paragraph mark of the
section break) and applied the normal Style to the whole section break
and hurra, it is working as desired.

Again, thank you for your valuable help

Regards,

Andreas
 
L

Lene Fredborg

Thank you for the feedback. I am glad to hear that the problem is solved.

--
Regards
Lene Fredborg
DocTools – Denmark
www.thedoctools.com
Document automation – add-ins, macros and templates for Microsoft Word
 
H

Helmut Weber

Hi Andreas,

how about this one:

Sub Test0001()
Dim rngDcm As Range ' range document
Dim rngTmp As Range ' range temporary
Dim lngSc1 As Long ' count sections
Dim lngSc2 As Long ' count sections
Set rngDcm = ActiveDocument.Range
Set rngTmp = selection.Range
With rngDcm.Find
.Style = "Heading 3"
If .Execute Then
rngTmp.start = 0
rngTmp.End = rngDcm.End
rngDcm.Select ' for testing
lngSc1 = rngTmp.Sections.Count
End If
Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Style = "Heading 3"
.Forward = False
If .Execute Then
rngDcm.Select ' for testing
rngTmp.start = 0
rngTmp.End = rngDcm.End
lngSc2 = rngTmp.Sections.Count
End If
End With
End With
MsgBox "Heading 3 first in section: " & lngSc1 & Chr(13) _
& "Heading 3 last in section: " & lngSc2
End Sub


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

andreas

Hey Helmut,

very nice macro coding. Superfast and very few macro lines.

Thank you very much

Andreas
 

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