View Single Post
 
Old 02-10-2016, 10:31 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 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