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
Stefan Blom - 08 Feb 2007 11:34 GMT
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.

Signature
Stefan Blom
Microsoft Word MVP
> Hello,
>
[quoted text clipped - 44 lines]
>
> Andreas
andreas - 09 Feb 2007 16:20 GMT
> 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,
[quoted text clipped - 61 lines]
>
> - 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
Stefan Blom - 12 Feb 2007 10:08 GMT
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.

Signature
Stefan Blom
Microsoft Word MVP
> > 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,
[quoted text clipped - 89 lines]
> Next s
> End Sub
Rob - 08 Feb 2007 17:32 GMT
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
andreas - 09 Feb 2007 16:25 GMT
> 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
[quoted text clipped - 15 lines]
>
> 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