Pointing an object to <current header>

M

Marat

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.
 
S

Stefan Blom

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


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

Marat

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.
****************************
 
S

Shauna Kelly

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
 
M

Marat

Hi Shauna,

The code is running in Access VBA. I am creating a brand-new document and
paint pages one at a time. The code stops at the bottom of a page and the
user can generate a next page with a button click.

The header of the next page may be different from previous header. I
therefore start every page with a section break. When I insert a new section
break, the newly created section inherits the header from the previous
section (linked to previous).

Unless I know the header type, I can't select it directly. That's why I
execute ".View.SeekView = wdSeekCurrentPageHeader", the header I end up in
could be of any type. I point my objectvar to it and manipulate the header
directly.

I hope this makes sense. :)

Thank you,
Marat.
 
M

Marat

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.

"
 
M

Marat

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.
 
M

Marat

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.
 
S

Shauna Kelly

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
 
M

Marat

Shauna,

Thank you much for this example - definitly helps me reach my goal using
this strategy.

I will look into STYLEREF too.

Marat.
 

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