MsgBox listing all sections that only carry a heading level 1

A

andreas

Dear Experts:

Below macro searches for sections that only have built-in heading
level 1 and no subsequent lower heading levels and sets a specific
header for them. It is running fine.

I wonder whether it is possible to expand the macro to show those
sections (only featuring heading 1 and no other subsequent lower
heading levels) in a MsgBox, such as:

Section 5, Summary
Section 6, Bibliography

The MsgBox should pop up in the place specified below.

Help is much appreciated. Thank you very much in advance.

Kind Regards, Andreas


Sub SetHeaderSectionsHeading1()
Dim s As Integer
Dim sect As Section
Dim HasHeading1() As Boolean
Dim HasHeading2() As Boolean
ReDim HasHeading1(ActiveDocument.Sections.count)
ReDim HasHeading2(ActiveDocument.Sections.count)
Dim rng As range

If MsgBox("This macro sets a specific header for all sections
featuring just heading level 1" & vbCrLf & _
" .... Would you like to continue?", vbYesNo + vbQuestion,
"Header for sections with just heading 1") = vbNo Then
Exit Sub
End If

Set rng = ActiveDocument.range
With rng.Find
.Format = True
.Style = ActiveDocument.Styles(wdStyleHeading1)
Do While .Execute(Wrap:=wdFindStop)
HasHeading1(rng.Sections(1).Index) = True
Loop
End With

Set rng = ActiveDocument.range
With rng.Find
.Format = True
.Style = ActiveDocument.Styles(wdStyleHeading2)
Do While .Execute(Wrap:=wdFindStop)
HasHeading2(rng.Sections(1).Index) = True
Loop
End With


For s = 1 To ActiveDocument.Sections.count
Set sect = ActiveDocument.Sections(s)

If HasHeading1(s) And Not HasHeading2(s) Then

<MsgBox listing these sections should go in here>


Set rng = sect.Headers(wdHeaderFooterPrimary).range

'Do processing page headers

End If

Next s

End Sub
 
D

Doug Robbins - Word MVP

I realise that this is not an answer to your question, but maybe the
STYLEREF field would do the same thing as your macro is doing at the moment.

To answer the specific question

Dim hSections as String
hSections = ""
For s = 1 To ActiveDocument.Sections.count
Set sect = ActiveDocument.Sections(s)
If HasHeading1(s) And Not HasHeading2(s) Then
hSections = hSections & "Section" & s &", "
Set rng = sect.Headers(wdHeaderFooterPrimary).range
'Do processing page headers
End If
Next s
hSections = Left(hSections, Len(hSections-2) 'strip off the final ", "
MsgBox "The following Sections have only a Heading1:- " & hSections

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

andreas

I realise that this is not an answer to your question, but maybe the
STYLEREF field would do the same thing as your macro is doing at the moment.

To answer the specific question

    Dim hSections as String
    hSections = ""
    For s = 1 To ActiveDocument.Sections.count
    Set sect = ActiveDocument.Sections(s)
         If HasHeading1(s) And Not HasHeading2(s) Then
            hSections = hSections & "Section" & s &", "
           Set rng = sect.Headers(wdHeaderFooterPrimary).range
            'Do processing page headers
        End If
    Next s
    hSections = Left(hSections, Len(hSections-2) 'strip off the final ", "
    MsgBox "The following Sections have only a Heading1:- " & hSections

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
























- Zitierten Text anzeigen -

Hey Doug,

thank you for your quick help. It is working fine. Is it also possible
to show the text of the section headings (Heading 1) in the msgbox?

Regards, Andreas
 
D

Doug Robbins - Word MVP

I am not sure what you would do with it in the message box or what you are
really trying to achieve, but possibly the use of the StyleRef field in a
First Page Header/Footer of the Section would do what you want.

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

I realise that this is not an answer to your question, but maybe the
STYLEREF field would do the same thing as your macro is doing at the
moment.

To answer the specific question

Dim hSections as String
hSections = ""
For s = 1 To ActiveDocument.Sections.count
Set sect = ActiveDocument.Sections(s)
If HasHeading1(s) And Not HasHeading2(s) Then
hSections = hSections & "Section" & s &", "
Set rng = sect.Headers(wdHeaderFooterPrimary).range
'Do processing page headers
End If
Next s
hSections = Left(hSections, Len(hSections-2) 'strip off the final ", "
MsgBox "The following Sections have only a Heading1:- " & hSections

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
























- Zitierten Text anzeigen -

Hey Doug,

thank you for your quick help. It is working fine. Is it also possible
to show the text of the section headings (Heading 1) in the msgbox?

Regards, Andreas
 
A

andreas

I am not sure what you would do with it in the message box or what you are
really trying to achieve, but possibly the use of the StyleRef field in a
First Page Header/Footer of the Section would do what you want.

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









Hey Doug,

thank you for your quick help. It is working fine. Is it also possible
to show the text of the section headings (Heading 1) in the msgbox?

Regards, Andreas- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Hey Doug,

thank you for your help. I solved the problem myself.
Regards, 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