A
andreas
Dear Experts:
Below macro sets a running header (alternately chapter title level 1
and 2 as well as page number) for ALL sections. It is running just
fine.
How do I have to change the macro so that ONLY the selected section
(where the cursor currently resides) gets a running header and NOT all
the sections?
The exact requirement is as follows:
A msgbox is to inform that the cursor currently resides in SECTION X
and will get a running header after pressing ok. Is this possible
provided that the 'range object' for the setting of the headers is
still used?.
Help is much appreciated. Thank you very much in advance. Regards,
Andreas
Sub Set_Running_Headers_All_Sect()
Dim rng As range
Dim sect As Section
If MsgBox("This macro inserts a running header for all sections" &
vbCrLf & _
"Would you like to continue?", vbYesNo + vbInformation,
"Alternating headers for main sections (e.g. 1 Implementation, 1.1
Analysis)") = vbNo Then
Exit Sub
Else
For Each sect In ActiveDocument.Sections
'Different odd- and even-page and first page headers for the whole
document
sect.PageSetup.OddAndEvenPagesHeaderFooter = True
sect.PageSetup.DifferentFirstPageHeaderFooter = True
Set rng = sect.range
'Get the start of the section
rng.Collapse wdCollapseStart
If rng.Information(wdActiveEndAdjustedPageNumber) Mod 2 = 1
Then
'MOD gets the remainder after dividing by 2
'If it's 0, then it's an even page number
'If the first first page header starts on an even page number
the
'headers are set as follows
'DO FIRST PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterFirstPage).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:="STYLEREF 1 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 1 "
'DO EVEN PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterEvenPages).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:="STYLEREF 1 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
'Alternatively spaces can be replaced with a tab stop
'rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
'rng.Text = vbTab
'rng.Text = "Seite "
rng.Collapse wdCollapseStart
rng.Text = vbTab
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 1 "
ActiveDocument.UndoClear
'DO ODD PAGE HEADERS
sect.Headers(wdHeaderFooterPrimary).PageNumbers.NumberStyle =
wdPageNumberStyleArabic
sect.Headers(wdHeaderFooterPrimary).PageNumbers.IncludeChapterNumber
= False
Set rng = sect.Headers(wdHeaderFooterPrimary).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF 2 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
'rng.Text = " "
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 2 "
Else
'DO FIRST PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterFirstPage).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF 2 \n "
rng.Collapse wdCollapseEnd
rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = " "
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 2 "
'DO EVEN PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterEvenPages).range
sect.Headers(wdHeaderFooterEvenPages).PageNumbers.NumberStyle =
wdPageNumberStyleArabic
sect.Headers
(wdHeaderFooterEvenPages).PageNumbers.IncludeChapterNumber = False
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF 2 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
'rng.Text = " "
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 2 "
ActiveDocument.UndoClear
'DO ODD PAGE HEADERS
sect.Headers(wdHeaderFooterPrimary).PageNumbers.NumberStyle =
wdPageNumberStyleArabic
sect.Headers(wdHeaderFooterPrimary).PageNumbers.IncludeChapterNumber =
False
Set rng = sect.Headers(wdHeaderFooterPrimary).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:="STYLEREF 1 \n"
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 1 "
End If
Next sect
End If
Application.ScreenUpdating = True
End Sub
Below macro sets a running header (alternately chapter title level 1
and 2 as well as page number) for ALL sections. It is running just
fine.
How do I have to change the macro so that ONLY the selected section
(where the cursor currently resides) gets a running header and NOT all
the sections?
The exact requirement is as follows:
A msgbox is to inform that the cursor currently resides in SECTION X
and will get a running header after pressing ok. Is this possible
provided that the 'range object' for the setting of the headers is
still used?.
Help is much appreciated. Thank you very much in advance. Regards,
Andreas
Sub Set_Running_Headers_All_Sect()
Dim rng As range
Dim sect As Section
If MsgBox("This macro inserts a running header for all sections" &
vbCrLf & _
"Would you like to continue?", vbYesNo + vbInformation,
"Alternating headers for main sections (e.g. 1 Implementation, 1.1
Analysis)") = vbNo Then
Exit Sub
Else
For Each sect In ActiveDocument.Sections
'Different odd- and even-page and first page headers for the whole
document
sect.PageSetup.OddAndEvenPagesHeaderFooter = True
sect.PageSetup.DifferentFirstPageHeaderFooter = True
Set rng = sect.range
'Get the start of the section
rng.Collapse wdCollapseStart
If rng.Information(wdActiveEndAdjustedPageNumber) Mod 2 = 1
Then
'MOD gets the remainder after dividing by 2
'If it's 0, then it's an even page number
'If the first first page header starts on an even page number
the
'headers are set as follows
'DO FIRST PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterFirstPage).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:="STYLEREF 1 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 1 "
'DO EVEN PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterEvenPages).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:="STYLEREF 1 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
'Alternatively spaces can be replaced with a tab stop
'rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
'rng.Text = vbTab
'rng.Text = "Seite "
rng.Collapse wdCollapseStart
rng.Text = vbTab
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 1 "
ActiveDocument.UndoClear
'DO ODD PAGE HEADERS
sect.Headers(wdHeaderFooterPrimary).PageNumbers.NumberStyle =
wdPageNumberStyleArabic
sect.Headers(wdHeaderFooterPrimary).PageNumbers.IncludeChapterNumber
= False
Set rng = sect.Headers(wdHeaderFooterPrimary).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF 2 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
'rng.Text = " "
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 2 "
Else
'DO FIRST PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterFirstPage).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF 2 \n "
rng.Collapse wdCollapseEnd
rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = " "
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 2 "
'DO EVEN PAGE HEADERS
Set rng = sect.Headers(wdHeaderFooterEvenPages).range
sect.Headers(wdHeaderFooterEvenPages).PageNumbers.NumberStyle =
wdPageNumberStyleArabic
sect.Headers
(wdHeaderFooterEvenPages).PageNumbers.IncludeChapterNumber = False
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"STYLEREF 2 \n "
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
'rng.Text = " "
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 2 "
ActiveDocument.UndoClear
'DO ODD PAGE HEADERS
sect.Headers(wdHeaderFooterPrimary).PageNumbers.NumberStyle =
wdPageNumberStyleArabic
sect.Headers(wdHeaderFooterPrimary).PageNumbers.IncludeChapterNumber =
False
Set rng = sect.Headers(wdHeaderFooterPrimary).range
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:="STYLEREF 1 \n"
rng.Collapse wdCollapseEnd
rng.Text = " "
rng.Text = vbTab
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
"PAGE \* Arabic "
rng.Text = vbTab
rng.Collapse wdCollapseStart
rng.Fields.Add rng, Type:=wdFieldEmpty, Text:= _
" STYLEREF 1 "
End If
Next sect
End If
Application.ScreenUpdating = True
End Sub