Roi
Roi

Reputation: 565

How to get Page Header not Section Headers in VBA

I'm struggling to get the page header from a word document with hundreds of pages, it has different headers

my code:

pageHeader = wdDoc.Sections(pageNumber).Headers(1).Range.Text

this code gets the headers for the Section but not spefic header of the page. Therefor there are chances that the header is incorrect.

my only input is the pageNumber

context: I'm importing word document to excel and each word page has tables, these tables are associated with their unique number apparently placed in the header of each page, so different pages can have different header

Upvotes: 0

Views: 933

Answers (1)

macropod
macropod

Reputation: 13505

Try something along the lines of:

wdDoc.Range.GoTo(What:=wdGoToPage, Name:=i).Sections.First.Headers(wdHeaderFooterPrimary).Range.Text

where i is the page number.

Note: The above code assumes you're using early binding. For late binding, use something along the lines of:

wdDoc.Range.GoTo(1, i).Sections.First.Headers(1).Range.Text

Upvotes: 2

Related Questions