Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-10-2016, 03:51 AM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default Find out if Page of Heading 1 has header

Hi,

I am trying to find using VBA weather when I have a HEading 1 - the page has a header. I want to make sure it doesn't.

I tried:
Code:
 
 sectionNum = .Information(wdActiveEndSectionNumber)
 Set secTemp = ActiveDocument.Sections(sectionNum)
                             
 If secTemp.Headers(wdHeaderFooterFirstPage).Exists = True Then
'page has header"
End If
But I always get True.

Is there another field that could show me whether the page has a header?



Thanks,
Rocky
Reply With Quote
  #2  
Old 02-10-2016, 07:01 AM
gmayor's Avatar
gmayor gmayor is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

The first page header will exist, even if empty, provided the different first page option is applied to the current section. I suspect what you want is to ensure the first page header has no content. In that case simply deleting the content of the header will achieve the aim of having an empty header e.g. as follows. I have added optional code to show if the header is empty.
Code:
Sub ClearHeader()
Dim secTemp As Section
Dim sectionNum As Integer
Dim oHeader As HeaderFooter
    sectionNum = Selection.Information(wdActiveEndSectionNumber)
    Set secTemp = ActiveDocument.Sections(sectionNum)

    If secTemp.Headers(wdHeaderFooterFirstPage).Exists = True Then
        Set oHeader = secTemp.Headers(wdHeaderFooterFirstPage)
        If Len(oHeader.Range) > 1 Then 'Optional
            oHeader.Range.Delete
        Else 'optional
            MsgBox "Header is empty" 'optional
        End If 'Optional
    End If
End Sub
__________________
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-10-2016, 01:13 PM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Smile Thanks!

Thanks. It worked.
Reply With Quote
  #4  
Old 02-10-2016, 09:44 PM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default One more question - hierarcy for the header text seems deeper

It did work once. But then, it was empty and to get to the header text I had to go to:
oHeader.Range.Sections.First.Headers.Item(1).Range .Text

does that make sense?
Is there a more "proper" way to get to this info or at least to Item(1)?
Reply With Quote
  #5  
Old 02-10-2016, 10:31 PM
gmayor's Avatar
gmayor gmayor is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

What EXACTLY are you trying to do? You don't have to empty the range to write to it, you can simply write something else to the range. Set a variable for the particular header you require (there are potentially three header ranges for each section) and write to it. The following shows how all asections and all headers in those sections can be addressed, if they exist:
Code:
Sub HeadersTest()
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oRng As Range
Dim strHeaderType As String
    For Each oSection In ActiveDocument.Sections
        For Each oHeader In oSection.Headers
            If oHeader.Exists Then
                Select Case oHeader.Index
                    Case 1: strHeaderType = "Primary"
                    Case 2: strHeaderType = "First Page"
                    Case 3: strHeaderType = "Even Pages"
                End Select
                Set oRng = oHeader.Range
                MsgBox "Section " & oSection.Index & vbCr & _
                       "Header " & strHeaderType & vbCr & _
                       "Contains: " & oRng.Text
                oRng.Text = "New header text for Section " & oSection.Index & " Header " & strHeaderType
                MsgBox "Section " & oSection.Index & " Header " & strHeaderType & " updated to " & vbCr & _
                       oRng.Text
            End If
        Next oHeader
    Next oSection
lbl_Exit:
    Set oSection = Nothing
    Set oHeader = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
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
  #6  
Old 02-11-2016, 04:06 AM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

I didn't explain properly what I am trying to do.
I need to search for each Heading 1 in the document and then see that there is no header on the same page.
oHeader.Range always seems to be an empty string even if there is a header on the page.
In contrast, oHeader.Range.Sections.First.Headers.Item(1).Range .Text always has a value even if there is no header on the page.
Reply With Quote
  #7  
Old 02-11-2016, 05:16 AM
gmayor's Avatar
gmayor gmayor is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

The macro I posted earlier today will give you a message box for every header in the document, which indicates which header it is, in which section of the document and whatever the text content of that header is. For a new blank document you should see only one message box. For a complex document you might see several.

oHeader.Range is a range not a text string. Of you want the text it contains then you want oHeader.Range.Text, but first you will have to tell the macro what oHeader refers to.

When you say that you want to search for each 'Heading 1' then can we assume that you mean a paragraph with the Heading 1 paragraph style?

The variation below will locate every Heading 1 style, and will report the contents of the headers of the section where that style is found. If there is content in the header you will be given an option to delete it.

You can use the techniques to do whatever you want with the headers in your document.
Code:
Sub Macro1()
Dim oRng As Range
Dim i As Integer
Dim iYes As Integer
Dim strText As String
Dim oHeader As HeaderFooter
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .Style = "Heading 1"
        Do While .Execute
            i = oRng.Information(wdActiveEndSectionNumber)
            For Each oHeader In ActiveDocument.Sections(i).Headers
                If oHeader.Exists Then
                    strText = oHeader.Range.Text
                    If Len(strText) > 1 Then
                        Select Case oHeader.Index
                            Case 1:        'Primary
                                iYes = MsgBox("The primary header content is " & oHeader.Range.Text & vbCr & _
                                              "Delete?", vbYesNo)
                                If iYes = vbYes Then oHeader.Range.Delete
                            Case 2:        'First Page
                                iYes = MsgBox("The first page header content is " & oHeader.Range.Text & vbCr & _
                                              "Delete?", vbYesNo)
                                If iYes = vbYes Then oHeader.Range.Delete
                            Case 3:        'Even Pages0
                                iYes = MsgBox("The even pages header content is " & oHeader.Range.Text & vbCr & _
                                              "Delete?", vbYesNo)
                                If iYes = vbYes Then oHeader.Range.Delete
                        End Select
                    Else
                        Select Case oHeader.Index
                            Case 1:        'Primary
                                MsgBox "The primary header is empty"
                            Case 2:        'First Page
                                MsgBox "The first page header is empty"
                            Case 3:        'Even Pages0
                                MsgBox "The even pages header is empty"
                        End Select
                    End If
                End If
            Next oHeader
            oRng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Set oHeader = Nothing
    Exit Sub
End Sub
__________________
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
  #8  
Old 02-11-2016, 06:40 AM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

Thank you for your patience.

I have 2 places in the document in which I have a header in a page with a heading that has a style of Heading 1.
In those places I get the same thing:
If Len(strText) > 1 Select Case oHeader.Index
Case 1: 'Primary

and then
else Case oHeader.Index
Case 2: 'First Page
MsgBox "The first page header is empty"

I can't see any difference in the behavior on a page in which I have Heading 1 and a header vs. a page that has a Heading 1 with no header displayed.

Is there another hidden variable I can use?

Thanks,
Rocky
Reply With Quote
  #9  
Old 02-11-2016, 07:27 AM
gmayor's Avatar
gmayor gmayor is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

The headers are attributes of the section the page is in, not of the page itself.
I would have to see the document to establish what it is that you are seeing and what you need to see, for I have covered all bases with the code sequences already posted.
__________________
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-14-2016, 02:08 AM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default How to check if current selection is section break

Hi,

As a temporary workaround for something else, trying to see if the line above my Heading 1 has a section break.
When I search in Word for "^b" it finds a section break.
However, selection.text has something that looks like an up arrow.
Is there a way that I can compare the selection to a section break or wdSectionBreakNextPage?

Thanks,
Rocky
Reply With Quote
  #11  
Old 02-14-2016, 02:15 AM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

Could it be that the section number is not currect?
I use:
sectionNum = .Information(wdActiveEndSectionNumber)
Set secTemp = ActiveDocument.Sections(sectionNum)

This occurs when the correct heading is selected.
However, when I look at the text it has the text of another heading.
When I step through the document I see that the section numbers increase.

I also tried to see if the line above has a section break. I can't seem to compare to "^b". Could you do that?
Reply With Quote
  #12  
Old 02-14-2016, 03:35 AM
macropod's Avatar
macropod macropod is offline Find out if Page of Heading 1 has header Windows 7 64bit Find out if Page of Heading 1 has header Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Rocky2: Please don't ask the same question in multiple threads. I've merged the two threads concerned.

Section numbers returned by .Information(wdActiveEndSectionNumber) will be correct. whether you're Set secTemp = ActiveDocument.Sections(sectionNum) is 'correct' depends on what you're referencing for .Information(wdActiveEndSectionNumber). Since you haven't posted the relevant code, or said what you're expecting, there's know way for anyone here to know one way or the other.

Why do you want to check the character before the Heading? Graham has already given you robust code that works for all headers, regardless of whether the preceding character is a Section break.

FWIW, Section breaks & page breaks have an ASCII value of 12, whilst column breaks have an ASCII value of 14.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 02-15-2016, 12:50 AM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

Sorry. It wasn't the same question so I thought it needed to be on a separate thread.

The format of the section was correct. Because there was no section break before some of the header 1s the header was inserted even though the definition of the section was correct.
I ended up checking if there was a header on the page and if there was in inserted a section break before the heading 1.
Thanks for your help.
Reply With Quote
Reply

Tags
headers, heading 1, section breaks



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find out if Page of Heading 1 has header Can't remove page break between a Heading 1 and a Heading 2 Nathan8752 Word 3 08-31-2015 12:41 PM
Find out if Page of Heading 1 has header Can I unter the last heading in a footer, regardless of what level the header is? Huffle Word 2 09-28-2014 03:48 PM
include heading text in header eNGiNe Word 2 03-06-2013 12:24 AM
How to have a heading 1 file automatically appear in each header of any page? expert4knowledge Word 2 09-16-2012 10:38 AM
Find out if Page of Heading 1 has header Need to change text in header to next Heading 1 Kanith Word 1 06-14-2011 03:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:12 AM.


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