Checking whether the current header is blank.

A

andreas

Dear Experts:
I would like to check whether the current header is blank (selection
object) The result should is to be displayed in a msgbox (Header is
blank or is not blank). Blank means that there are no characters or
fields in the header. Blank Spaces count as blank and not as
characters.

Thank you very much in advance. Regards, Andreas
 
L

Lene Fredborg

The following macro may do what you want. Note that I have included check of
all three header types of the (first) section of the current selection:


Sub CheckIsHeaderBlank()
Dim Msg As String
Dim strHeader_Primay As String
Dim strHeader_FirstPage As String
Dim strHeader_Even As String

'Start creating Msg
Msg = ""

With Selection.Sections(1)
'Check all three types of headers in current section
'Remove any spaces - regard as empty if remaining string is ""
strHeader_Primay =
Replace(.Headers(wdHeaderFooterPrimary).Range.Text, " ", "")
strHeader_FirstPage =
Replace(.Headers(wdHeaderFooterFirstPage).Range.Text, " ", "")
strHeader_Even =
Replace(.Headers(wdHeaderFooterEvenPages).Range.Text, " ", "")
End With

'Construct Msg
'Header is empty if string consists of Chr(13) - paragraph mark
If strHeader_Primay = Chr(13) Then
Msg = Msg & "Primary header:" & vbTab & "Empty" & vbCr
Else
Msg = Msg & "Primary header:" & vbTab & "Not empty" & vbCr
End If

If strHeader_FirstPage = Chr(13) Then
Msg = Msg & "First page header:" & vbTab & "Empty" & vbCr
Else
Msg = Msg & "First page header:" & vbTab & "Not empty" & vbCr
End If

If strHeader_Even = Chr(13) Then
Msg = Msg & "Even page header:" & vbTab & "Empty" & vbCr
Else
Msg = Msg & "Even page heade:" & vbTab & "Not empty" & vbCr
End If

'Show Msg
MsgBox Msg, vbOKOnly, "Info About Current Header"

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

The following macro may do what you want. Note that I have included checkof
all three header types of the (first) section of the current selection:

Sub CheckIsHeaderBlank()
    Dim Msg As String
    Dim strHeader_Primay As String
    Dim strHeader_FirstPage As String
    Dim strHeader_Even As String

    'Start creating Msg
    Msg = ""

    With Selection.Sections(1)
        'Check all three types of headers in current section
        'Remove any spaces - regard as empty if remaining string is ""
        strHeader_Primay =
Replace(.Headers(wdHeaderFooterPrimary).Range.Text, " ", "")
        strHeader_FirstPage =
Replace(.Headers(wdHeaderFooterFirstPage).Range.Text, " ", "")
        strHeader_Even =
Replace(.Headers(wdHeaderFooterEvenPages).Range.Text, " ", "")
    End With

    'Construct Msg
    'Header is empty if string consists of Chr(13) - paragraph mark
    If strHeader_Primay = Chr(13) Then
        Msg = Msg & "Primary header:" & vbTab & "Empty" & vbCr
    Else
        Msg = Msg & "Primary header:" & vbTab & "Not empty" & vbCr
    End If

    If strHeader_FirstPage = Chr(13) Then
        Msg = Msg & "First page header:" & vbTab & "Empty" & vbCr
    Else
        Msg = Msg & "First page header:" & vbTab & "Not empty" & vbCr
    End If

    If strHeader_Even = Chr(13) Then
        Msg = Msg & "Even page header:" & vbTab & "Empty" & vbCr
    Else
        Msg = Msg & "Even page heade:" & vbTab & "Not empty" & vbCr
    End If

    'Show Msg
    MsgBox Msg, vbOKOnly, "Info About Current Header"

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word






- Zitierten Text anzeigen -

Dear Lene,

great coding. Thank you very much. Regards, Andreas
 
L

Lene Fredborg

You are welcome.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top