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.

Finding sections automatically that only contain heading 1 level

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andreas - 08 Feb 2007 08:51 GMT
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
 
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.