Shauna
Sun Oct 08 03:15:56 CDT 2006
Hi Marat
If I'm creating a document in code, which is to be manipulated in code
(rather than by a real-live user), I set every section's
.PageSetup.DifferentFirstPageHeaderFooter to True, and
.PageSetup.OddAndEvenPagesHeaderFooter to True.
And, for all 3 headers and all three footers, I set .LinkToPrevious to
False.
Now, I know that I'm using all 3 headers and all 3 footers for every section
and they're all independent of one another. That would make for an unwieldy
document to use through the UI, but I find it a lot easier to deal with in
code.
So, to get the "current" header, I only need to know the section and whether
this is the first page, an even page or an odd page in the section.
This is a bit laborious, but shows how to do that:
Sub GetCurrentHeader()
Dim rngSelection As Word.Range
Dim lngPageNumber As Long
Dim lngSecNumber As Long
Dim hdCurrent As Word.HeaderFooter
'Get a reference to the selected range
Set rngSelection = Selection.Range
'The current selection may span more than one
'page, so collapse it
rngSelection.Collapse wdCollapseStart
'Get the current section and page numbers
lngSecNumber = rngSelection.Sections(1).Index
lngPageNumber = rngSelection.Information(wdActiveEndAdjustedPageNumber)
With ActiveDocument.Sections(lngSecNumber)
If lngPageNumber = 1 Then
Set hdCurrent = .Headers(wdHeaderFooterFirstPage)
ElseIf lngPageNumber Mod 2 = 0 Then
Set hdCurrent = .Headers(wdHeaderFooterEvenPages)
Else
Set hdCurrent = .Headers(wdHeaderFooterPrimary)
End If
End With
MsgBox hdCurrent.Range.Text
End Sub
Now, having done all of that, one has to ask whether it's necessary to use a
different section for each page. Would a STYLEREF field solve the problem?
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
"Marat" <Marat@discussions.microsoft.com> wrote in message
news:9293FD54-9FE9-43FC-A968-890B9E270296@microsoft.com...
> Shauna,
>
> I am running this code from Access. The Word document is created from
> scratch, pages are created one at a time. The program stops at every page
> and user can choose to continue generating new pages.
>
> The headers could be different on new page, so I insert a section break on
> every page. Once new section starts, it inherits the header type from
> previous section, but I don't know what the type of the inherited header
> is.
> If I knew the type, I would point the object to it directly. Instead
> since I
> don't know header type, I need to move the cursor by ".View.SeekView =
> wdSeekCurrentPageHeader" and then point to the header with
> ".Headers(wdHeaderFooterPrimary)".
>
> So, basically I want to determine what is the header type of the current
> page without moving the cursor to the header.
>
> Thank you,
> Marat.
>
> "
> "Shauna Kelly" wrote:
>
>> Hi Marat
>>
>> Two quick questions, that might help us understand what you're trying to
>> do:
>>
>> 1. Are you working with a document that already exists, that might have
>> come
>> from anywhere? Or, are you working with a document that you have some
>> control over? That is, a document where you could decide what headers and
>> footers the document will have?
>>
>> 2. What happens when all your code is finished? Is this code running, for
>> example, when a new document is created? Or can a user run this code
>> while
>> in the middle of working on a document?
>>
>> Shauna
>>
>> Shauna Kelly. Microsoft MVP.
>>
http://www.shaunakelly.com/word
>>
>>
>> "Marat" <Marat@discussions.microsoft.com> wrote in message
>> news:A2F04A7A-5D70-44CE-934A-765FF43A6BF0@microsoft.com...
>> > Stefan,
>> >
>> > Thank you for your answer.
>> >
>> > When you say:
>> >
>> >> But note that you don't actually need to move the cursor to get the
>> >> header. Instead, use the following:
>> >>
>> >> Set myheader = <windowobject>.Selection _
>> >> .Sections(1).Headers(wdHeaderFooterPrimary)
>> >
>> > The problem is that I don't know which header type is being used in the
>> > current section. I have to jump into the current header, point an
>> > object
>> > to
>> > the current header. Only then I can determine what kind of header it
>> > is -
>> > First, OddEven, Primary.
>> >
>> > I therefore can't avoid moving the cursor into header.
>> >
>> > If I could determine the type of the current section header in use, I
>> > could
>> > point my object to that specific header without moving the cursor. I
>> > couldn't
>> > find how to do that.
>> >
>> > There 2 questions here:
>> >
>> > How to determine which header type is used in the current section?
>> >
>> > Could headers of different types exist simultaneously in the same
>> > section
>> > so
>> > I could "hide/show" the one I want instead of rewriting them for each
>> > section?
>> >
>> > Thank you,
>> > Marat.
>> > ****************************
>> >
>> >
>> > "Stefan Blom" wrote:
>> >
>> >> After the insertion point is moved to the header:
>> >>
>> >> <windowobject>.ActivePane.View.SeekView = wdSeekCurrentPageHeader
>> >>
>> >> use the following:
>> >>
>> >> Set myheader = <windowobject>.Selection.HeaderFooter
>> >>
>> >> to get the current header (no matter if it is an odd page, even page,
>> >> or first page header).
>> >>
>> >> But note that you don't actually need to move the cursor to get the
>> >> header. Instead, use the following:
>> >>
>> >> Set myheader = <windowobject>.Selection _
>> >> .Sections(1).Headers(wdHeaderFooterPrimary)
>> >>
>> >> The last example uses the Headers collection (so that you can get any
>> >> header in the current section).
>> >>
>> >> --
>> >> Stefan Blom
>> >> Microsoft Word MVP
>> >>
>> >>
>> >> "Marat" wrote in message
>> >> news:C3B503E9-E3B6-4C65-B1E7-42975EC8B392@microsoft.com...
>> >> > Hello,
>> >> >
>> >> > I need to point an object to the current page header. If d is my
>> >> active
>> >> > document, I do:
>> >> >
>> >> >
>> >> > Dim MyHeader As Word.HeaderFooter
>> >> >
>> >> > d.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
>> >> > Set MyHeader =
>> >> d.Sections(d.Sections.Count).Headers(wdHeaderFooterPrimary)
>> >> >
>> >> >
>> >> >
>> >> > The current header could be different type (not the primary, but
>> >> firstpage,
>> >> > oddpage, evenpage etc.). How can I point an object var to the
>> >> header in
>> >> > which the cursor is positioned after the View.SeekView has located
>> >> that
>> >> > header for me?
>> >> >
>> >> > Thank you in advance,
>> >> > Marat.
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>>
>>
>>