PDA

View Full Version : Solved: Detect whether a Word range is in header/footer



brengla
04-22-2010, 08:38 AM
Hi,

I was wondering if there is a way to detect, using VBA, whether a Word range is in the header or footer?

Thanks,
Bren

fumei
04-22-2010, 10:48 AM
Please clarify. A Word range must be declared, and defined by the programmer. They do not just happen by themselves. So, I do not understand the question. It is not possible to have a range WITHOUT knowing if it is in the header or footer.

Oh....wait...are you asking to know if it is one (the header) or the other (the footer)? Still, the answer is the same. A range is declared and explicitly defined.

brengla
04-23-2010, 01:50 AM
Sorry, I worded that badly.

Essentially what I have is a a bit of code like this ...


Private Sub Macro()

'Determine what to do if wrdRng is in a header or footer
If Selection.Range.IsInHeader Or Selection.Range.IsInFooter Then

MsgBox "The current selection is in a header or footer"

Else

MsgBox "The current selection is in the main body of the document"

End If

End Sub



Obviously I made up the .IsInHeader and .IsInFooter bits, but these are the bits I need help with. I just can't work out a way of determining whether the selected text is in the main body of the document or in the header/footer.

Hope this is a better description of what I was trying to say.

Thanks,
Bren

Tinbendr
04-23-2010, 04:27 AM
I believe Story Range is what you're looking for.

See if this (http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm) is what you're looking for.

brengla
04-23-2010, 06:03 AM
Got it working, thank you. For information, and in case anyone is curious ...


sub Macro()

Dim IsInHeaderOrFooter as Boolean
Dim CurrRng as Range
set CurrRng = Selection.Range

If CurrRng.StoryType = wdPrimaryFooterStory Or CurrRng.StoryType = wdPrimaryHeaderStory Then
IsInHeaderOrFooter = True
Else
IsInHeaderOrFooter = False
end if

end sub

fumei
04-26-2010, 12:39 PM
And...you were asking if the Selection is in the header.

This has nothing, zip, nada, to do with if "a range" is in the header.

As an aside, WHY do you need to know this? What difference does it make?

As an second aside, it is better to avoid using Selection, at all. 99% of the times it is neither needed, nor desireable.

fumei
04-26-2010, 12:53 PM
Here are the numeric values of all Storyranges.

MainTextStory = 1
FootnotesStory = 2
EndNotesStory = 3
CommentsStory = 4
TextFrameStory = 5
EvenPagesHeaderStory = 6
PrimaryFooterStory = 7
EvenPagesFooterStory = 8
PrimaryFooterStory = 9
FirstPageHeaderStory = 10
FirstPageFooterStory = 11

Therefore,Sub WhereTheHeckAm_I()
Select Case Selection.StoryType
Case 6, 7, 10
MsgBox "I am in a Header"
Case 8, 9, 11
MsgBox "I am in a Footer."
Case Else
MsgBox "I am NOT in either a header or a footer."
End Select
End Sub

fumei
04-26-2010, 12:58 PM
OR...................

Do it as Function.
Function WhereTheSelectionIs() As String
Select Case Selection.StoryType
Case 6, 7, 10
WhereTheSelectionIs = "The Selection is in a Header"
Case 8, 9, 11
WhereTheSelectionIs = "The Selection is in a Footer."
Case 1
WhereTheSelectionIs = "The Selection is in the Main Story."
Case Else
WhereTheSelectionIs = "The Selection is NOT in either a " & _
"header/footer or the Main Story."
End Select
End Function

Sub Whatever()
' do this
' do that
' find out if the Selection is in a header or footer or MainStory..or not
MsgBox WhereTheSelectionIs
End Sub

fumei
04-26-2010, 01:02 PM
And..............

If you need to know if it is in a header or footer for code purposes (which quite frankly is impossible to NOT know...), do the function as Boolean.

Function yeahInHeaderFooter() As Boolean
Select Case Selection.StoryType
Case 6, 7, 8, 9, 10, 11
yeahInHeaderFooter = True
Case Else
yeahInHeaderFooter = False
End Select
End Function

Sub Whatever()
' do this
' do that
' find out if the Selection is in a header or footer
If yeahInHeaderFooter = True Then
' yes it IS in a header/footer
' so....do WHAT exactly?
Else
' do whatever else it is if the Selection is NOT in a header/footer
End If
End Sub