Range.Information in section headers

M

Mike Clayton

Hi,

I'm having a bit of a problem with geting correct values for
Range.Information(wdVerticalPositionRelativeToPage) in section headers.

If I try this on a range object in the Headers and Footers of the first
section in a document it returns a valid result (e.g. "49.1"). It also works
if I try it in subsequent sections if they are linked to the previous section
all the way back to section 1. However when I try it on a range in a section
header which doesn't use Link to Previous
myRange.Information(wdVerticalPositionRelativeToPage) simply returns -1.

Does anyone know any way to work around this? It's causing me no end of
grief...

-- Mike
 
M

Mike Clayton

The following code reproduces the behaviour. On my machine it gives a
vertical position of 35.35 for section 1, 35.35 for section 2 linked to
section 1, and then -1 for section 2 unlinked from section 1.

Public Sub TestSectionInformation()

Dim objDocument As Word.Document
Dim objHeader As Word.HeaderFooter

Set objDocument = New Word.Document

' test section 1
Set objHeader = objDocument.Sections(1).Headers(wdHeaderFooterPrimary)
objHeader.Range.Text = "Header text for section 1"
MsgBox objHeader.Range.Text &
objHeader.Range.Information(wdVerticalPositionRelativeToPage), , "Section 1"

' test section 2 linked to previous
objDocument.Sections.Add
Set objHeader = objDocument.Sections(2).Headers(wdHeaderFooterPrimary)
MsgBox objHeader.Range.Text &
objHeader.Range.Information(wdVerticalPositionRelativeToPage), , "Section 2 -
Linked to previous"

' test section 2 unlinked from previous
objHeader.Range.Text = "Header text for section 2"
objHeader.LinkToPrevious = False
MsgBox objHeader.Range.Text &
objHeader.Range.Information(wdVerticalPositionRelativeToPage), , "Section 2 -
Unlinked from previous"

End Sub
 
H

Helmut Weber

Hi Mike,

sometimes previously switching views
from print layout to normal
and back or the other way round helps.

I don't know better.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
M

Mike Clayton

Hi Helmut,

Thanks for the reply. I've tried switching views and forcing a Repaginate,
but it doesn't seem to make any difference unfortunately.

M
 
T

Tony Jollans

This does appear to be a bug.

--
Enjoy,
Tony

Mike Clayton said:
The following code reproduces the behaviour. On my machine it gives a
vertical position of 35.35 for section 1, 35.35 for section 2 linked to
section 1, and then -1 for section 2 unlinked from section 1.

Public Sub TestSectionInformation()

Dim objDocument As Word.Document
Dim objHeader As Word.HeaderFooter

Set objDocument = New Word.Document

' test section 1
Set objHeader = objDocument.Sections(1).Headers(wdHeaderFooterPrimary)
objHeader.Range.Text = "Header text for section 1"
MsgBox objHeader.Range.Text &
objHeader.Range.Information(wdVerticalPositionRelativeToPage), , "Section
1"

' test section 2 linked to previous
objDocument.Sections.Add
Set objHeader = objDocument.Sections(2).Headers(wdHeaderFooterPrimary)
MsgBox objHeader.Range.Text &
objHeader.Range.Information(wdVerticalPositionRelativeToPage), , "Section
2 -
Linked to previous"

' test section 2 unlinked from previous
objHeader.Range.Text = "Header text for section 2"
objHeader.LinkToPrevious = False
MsgBox objHeader.Range.Text &
objHeader.Range.Information(wdVerticalPositionRelativeToPage), , "Section
2 -
Unlinked from previous"

End Sub
 
J

Jonathan West

Mike Clayton said:
Hi,

I'm having a bit of a problem with geting correct values for
Range.Information(wdVerticalPositionRelativeToPage) in section headers.

If I try this on a range object in the Headers and Footers of the first
section in a document it returns a valid result (e.g. "49.1"). It also
works
if I try it in subsequent sections if they are linked to the previous
section
all the way back to section 1. However when I try it on a range in a
section
header which doesn't use Link to Previous
myRange.Information(wdVerticalPositionRelativeToPage) simply returns -1.

Does anyone know any way to work around this? It's causing me no end of
grief...

The Information(wdVerticalPositionRelativeToPage) property returns -1 if the
relevant range is not visible on screen. When you have headers & footers
that use LinkToPrevious, the header & footer for that section doesn't
actually exist. Try making sure that the header or footer of the previous
section (or as many sections as you need to go back to find the original) is
visible on screen.
 
M

Mike Clayton

Hi Jonathan,

Thanks for the tip. It's certainly a bit more predictable doing as you
suggested, but it's still failing sometimes, and for no apparent reason. I
don't know whether it's individual documents that are causing it to failing,
or just Word being a bit cranky.

In any case, I think I'm going to have to abandon what I'm trying to do and
go back to the drawing board to come up with a different approach.

Thanks again,

Mike
 

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