Finding sections automatically that only contain heading 1 level

A

andreas

Hello,

I got a long word document with 8 sections. Almost all of the sections
contain headings level 1,2 and 3. Two of the sections only contain a
heading 1 level.

I now would like to write a macro that automatically finds the
sections where there is only a heading 1 level and set the headers for
these sections automatically.

FOR EXAMPLE, say Section 4 and 7 each contain one heading level 1 and
no headings
level 2 and 3.
The macro should first find these sections (section 4 and 7) and then
with this information do the following (the below part of the macro is
working fine):

Set sect = ActiveDocument.Sections(4)

Set rng = sect.Headers(wdHeaderFooterPrimary).Range
sect.Headers(wdHeaderFooterPrimary).LinkToPrevious = False

rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 1"" "
rng.Collapse wdCollapseEnd
rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Roman "


Set sect = ActiveDocument.Sections(7)

Set rng = sect.Headers(wdHeaderFooterPrimary).Range
sect.Headers(wdHeaderFooterPrimary).LinkToPrevious = False

rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF ""Heading 1"" "
rng.Collapse wdCollapseEnd
rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Roman "


Help is much appreciated. Thank you in advance

Regards,

Andreas
 
S

Stefan Blom

I guess you are trying to control which text is picked up by the
STYLEREF field? Note that you may not need a macro for this; instead,
you can create a character style (that doesn't alter the formatting),
apply it to the appropriate heading paragraphs, and then STYLEREF the
character style.

--
Stefan Blom
Microsoft Word MVP


in message
news:[email protected]...
 
R

Rob

If I'm reading this right you want to loop through all the sections and find
out if the section does not contain a heading 2. Correct? You could do
something like this. You could call your sub from the spot indicated.

Sub FindHeadings()
Dim i As Integer

For i = 1 To ActiveDocument.Sections.Count
ActiveDocument.Sections(i).Range.Select
With Selection.Find
.Style = wdStyleHeading2
.Execute
If .Found = False Then
'didn't find a heading 2 so do your thing here
End If
End With
Next i

End Sub
 
A

andreas

I guess you are trying to control which text is picked up by the
STYLEREF field? Note that you may not need a macro for this; instead,
you can create a character style (that doesn't alter the formatting),
apply it to the appropriate heading paragraphs, and then STYLEREF the
character style.

--
Stefan Blom
Microsoft Word MVP

in message















- Zitierten Text anzeigen -

Stefan,

Thank you for the answer. In the meantime I got the code I was looking
for (see below). I did not quite understand what you
tried to explain to me. Could you give me another practical example of
this Styleref of a character style?




Sub FindSections()
Dim s As Integer
Dim HasHeading2() As Boolean
ReDim HasHeading2(ActiveDocument.Sections.Count)
Dim rng As Range

Set rng = ActiveDocument.Range
With rng.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 2")
Do While .Execute(Wrap:=wdFindStop)
HasHeading2(rng.Sections(1).Index) = True
Loop
End With
For s = 1 To ActiveDocument.Sections.Count
If Not HasHeading2(s) Then
DoProcessing s
End If
Next s
End Sub
 
A

andreas

If I'm reading this right you want to loop through all the sections and find
out if the section does not contain a heading 2. Correct? You could do
something like this. You could call your sub from the spot indicated.

Sub FindHeadings()
Dim i As Integer

For i = 1 To ActiveDocument.Sections.Count
ActiveDocument.Sections(i).Range.Select
With Selection.Find
.Style = wdStyleHeading2
.Execute
If .Found = False Then
'didn't find a heading 2 so do your thing here
End If
End With
Next i

End Sub


Rob,

Thank you very much. Your code is working well. In the meantime I got
hold of another code. See below:

Sub FindSections()
Dim s As Integer
Dim HasHeading2() As Boolean
ReDim HasHeading2(ActiveDocument.Sections.Count)
Dim rng As Range

Set rng = ActiveDocument.Range
With rng.Find
.Format = True
.Style = ActiveDocument.Styles("Heading 2")
Do While .Execute(Wrap:=wdFindStop)
HasHeading2(rng.Sections(1).Index) = True
Loop
End With
For s = 1 To ActiveDocument.Sections.Count
If Not HasHeading2(s) Then
DoProcessing s
End If
Next s
End Sub
 
S

Stefan Blom

What I suggested was that you should create a character style with no
formatting [base it on "(underlying properties)"], apply it to
headings that you want included in a header (or footer), and finally
insert a { STYLEREF "char_style_name_here" } in the header (footer).
That way, you can control which text displays in the header (footer),
no matter what paragraph styles are actually present (on the page or
in the section).

For more on the STYLEREF field, see
http://sbarnhill.mvps.org/WordFAQs/StyleRef.htm.

--
Stefan Blom
Microsoft Word MVP


in message
 

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