#1
|
|||
|
|||
How to get all headers text in a Word Document ?
Hi,
I have a Word Document. Every page has a header. Right now I am only able to get the current page header. Eg. If the page is currently at page 3, it will only display page 3 header text in a MessageBox. The attached image is what I have tried. I was thinking is it I have to use a For loop to get all pages' header text? Thank you |
#2
|
||||
|
||||
There are potentially three headers in each section of the document. Your macro accesses one of them. If you want them all you need to loop through the sections and the headers in those sections e.g.
Code:
Dim oSection As Section Dim oHeader As HeaderFooter For Each oSection In ActiveDocument.Sections For Each oHeader In oSection.Headers If oHeader.Exists Then MsgBox oHeader.Range.Text End If Next oHeader Next oSection
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Hi, gmayor
I have tried the given codes. However, it still displays only one of the pages header text. The first MessageBox displays one of the pages header text. When I clicked 'Ok', the second MessageBox displays nothing and then it ends. I used Code:
MsgBox ActiveDocument.Sections.Count May I know what could be the reason? May I know why does the Word Document only have 1 section? Thank you Last edited by BeginnerLearner; 02-03-2019 at 06:09 PM. |
#4
|
|||
|
|||
One section or one hundred, each section has 3 headers (used or not).
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 02/03/2019 Dim lngIndex As Long Dim oSec As Section For Each oSec In ActiveDocument.Sections For lngIndex = 1 To 3 MsgBox oSec.Headers(lngIndex).Range.Text Next Next lbl_Exit: Exit Sub End Sub |
#5
|
|||
|
|||
Hi, gmayor
lngIndex means that it is going through the 3 headers for each section, that why you put 1 To 3? I have tried the above code and it still the same. The first MessageBox displays the header text. The second and third MessageBox display empty. Thank you |
#6
|
||||
|
||||
Post the document so we can view it. The macro which Greg posted is just a variation of what I posted and either will show the text content of the headers in each section.
If your macro shows only one section then there is only one section in the document. This should be confirmed by the Word status bar (right click the status bar and ensure that Sections is checked).
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Hi, gmayor
I have checked the Word status bar and it only shows 1. Attached is a sample Word Document. There are 3 pages and each page has a header. I want to get all header. However currently, I am only able to get one of the header text. May I know what is the reason? Thank you |
#8
|
|||
|
|||
My name is not gmayor. gmayor is the other person helping you.
Every section of a document has three headers 1) FirstPageHeader 2) PrimaryPageHeader and 3) EvenPageHeader. Your document has 1 section and that section is formatted with primary page header only. If you want to use all three headers then you need to format the page layout for different odd and even pages and different first page. Note: Even then you are only going to get three message boxes regardless if your 1 section document has one page or a thousand pages. |
#9
|
||||
|
||||
Your sample document has one section and uses ony the primary header which repeats on all three pages. That header contains a field that reflects the page number it appears on.
The macro I posted will display a single message showing that header, however as fields are not compatible with text displays that message will show Page 1.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#10
|
||||
|
||||
Every section contains three different headers and three different footers.
BUT you won't necessarily SEE them in your document unless: - Page Setup > Layout > Different odd and even is TURNED ON - Page Setup > Layout > Different first page is TURNED ON - and your section contains at least 3 pages If any of those are not true, then you won't be able to see the header or footer but it will still exist. Your sample document contains three pages but it has both the page setup layout options turned off. The VBA is reporting on two things you can't see - and the three pages you can see are all showing the same header/footer.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Hi, gmaxey
Sorry as both name look familiar thus I thought is gmayor Does that mean if I want to get all header, I have to find using Code:
wdHeaderFooterPrimaryPages Thank you |
#12
|
|||
|
|||
Hi gmayor
As my sample document uses only primary pages Does that mean in a For Loop, If header exists, I have to check if the header is PrimaryPageHeader before displaying in a MsgBox? Thank you |
#13
|
|||
|
|||
Hi, Guessed
Thanks for your help! As my sample document uses only primary pages If both the page setup layout options are turned off, will it affect anything or the outcome? Thank you |
#14
|
||||
|
||||
Obviously, if your document doesn't have a 'different first page' or 'different odd and even' page setup, you won't get any output for the corresponding headers.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
||||
|
||||
"the outcome" is an interesting term. We don't actually know what it is that you are trying to do. The information you have seen from me, Greg, Graham and Paul is all correct but perhaps not fulfilling what you really want because you haven't been clear on that.
Displaying a messagebox of the text string of a header is pretty pointless - it doesn't show formatting, graphics, tables, fields etc. If you can explain what exactly you want to do with the header content, perhaps we can tell you what you actually need to know.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Apply template with headers/footers to an existing Word document Files | qadeerahmed | Word | 5 | 05-08-2017 05:28 AM |
Headers & Footers - Show Document Text | Aprilann | Word | 6 | 04-13-2017 09:34 AM |
Word 2007 - different text for odd and even headers? | JQP | Word | 4 | 10-09-2011 06:49 PM |
How do I have headers and footers appear as regular text for the whole document? | user908045 | Word | 0 | 03-13-2010 12:41 PM |
Portrait & Landscape Headers in Same Word Document | Coast331 | Word | 0 | 10-24-2009 02:39 PM |