Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-03-2019, 07:42 AM
BeginnerLearner BeginnerLearner is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2013
Novice
How to get all headers text in a Word Document ?
 
Join Date: Feb 2019
Posts: 22
BeginnerLearner is on a distinguished road
Default 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
Attached Images
File Type: jpg HeaderText.jpg (32.8 KB, 55 views)
Reply With Quote
  #2  
Old 02-03-2019, 08:06 AM
gmayor's Avatar
gmayor gmayor is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 02-03-2019, 08:51 AM
BeginnerLearner BeginnerLearner is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2013
Novice
How to get all headers text in a Word Document ?
 
Join Date: Feb 2019
Posts: 22
BeginnerLearner is on a distinguished road
Default

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
to check the number of sections and it displays 1

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.
Reply With Quote
  #4  
Old 02-03-2019, 07:49 PM
gmaxey gmaxey is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 02-03-2019, 08:16 PM
BeginnerLearner BeginnerLearner is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2013
Novice
How to get all headers text in a Word Document ?
 
Join Date: Feb 2019
Posts: 22
BeginnerLearner is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 02-03-2019, 10:04 PM
gmayor's Avatar
gmayor gmayor is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #7  
Old 02-04-2019, 10:20 AM
BeginnerLearner BeginnerLearner is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2013
Novice
How to get all headers text in a Word Document ?
 
Join Date: Feb 2019
Posts: 22
BeginnerLearner is on a distinguished road
Default

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
Attached Files
File Type: docx Test_Practice.docx (13.2 KB, 18 views)
Reply With Quote
  #8  
Old 02-04-2019, 11:56 AM
gmaxey gmaxey is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 02-04-2019, 09:29 PM
gmayor's Avatar
gmayor gmayor is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #10  
Old 02-05-2019, 03:58 PM
Guessed's Avatar
Guessed Guessed is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #11  
Old 02-06-2019, 09:54 AM
BeginnerLearner BeginnerLearner is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2013
Novice
How to get all headers text in a Word Document ?
 
Join Date: Feb 2019
Posts: 22
BeginnerLearner is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 02-06-2019, 10:00 AM
BeginnerLearner BeginnerLearner is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2013
Novice
How to get all headers text in a Word Document ?
 
Join Date: Feb 2019
Posts: 22
BeginnerLearner is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 02-06-2019, 10:10 AM
BeginnerLearner BeginnerLearner is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2013
Novice
How to get all headers text in a Word Document ?
 
Join Date: Feb 2019
Posts: 22
BeginnerLearner is on a distinguished road
Default

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
Reply With Quote
  #14  
Old 02-06-2019, 01:11 PM
macropod's Avatar
macropod macropod is offline How to get all headers text in a Word Document ? Windows 7 64bit How to get all headers text in a Word Document ? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #15  
Old 02-06-2019, 03:03 PM
Guessed's Avatar
Guessed Guessed is offline How to get all headers text in a Word Document ? Windows 10 How to get all headers text in a Word Document ? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

"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
Reply With Quote
Reply

Thread Tools
Display Modes


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
How to get all headers text in a Word Document ? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:53 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft