Word VBA - Add text to header without overwriting existing content

CNorth

New Member
Joined
Jan 7, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi

I am trying to insert text into a header without overwriting the existing header. My document currently has a logo and a small table in the document header. The code below adds text in the required position but it overwrites the existing header. I wondered if it's possible to add text without overwriting? I've tried to search the web but can't find any info about this.


VBA Code:
Sub HdrTest()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set HeaderRange = ActiveDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
HeaderRange.Text = "Text 1" & vbTab & vbTab & "Text 2" & vbNewLine & "Text 3" & vbTab & vbTab & "Text 4"

With HeaderRange.Font
.Name = "Arial"
.Size = 8
End With

ActiveDocument.Sections(1).PageSetup.HeaderDistance = CentimetersToPoints(0.2)
End With

End Sub

Any help would be really appreciated.

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
VBA Code:
Set HeaderRange = ActiveDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
With HeaderRange
    .Collapse wdCollapseEnd
    .InsertAfter ("Text 1" & vbTab & vbTab & "Text 2" & vbNewLine & "Text 3" & vbTab & vbTab & "Text 4")
    With .Font
        .Name = "Arial"
        .Size = 8
    End With
End With
 
Upvote 0
That's as simple as:
VBA Code:
Sub HdrDemo()
Application.ScreenUpdating = False
With ActiveDocument.Sections.First
  With .Headers(wdHeaderFooterPrimary).Range
    .Collapse wdCollapseEnd
    .Text = "Text 1" & vbTab & vbTab & "Text 2" & vbCr & "Text 3" & vbTab & vbTab & "Text 4"
    .Font.Name = "Arial"
    .Font.Size = 8
  End With
  .PageSetup.HeaderDistance = CentimetersToPoints(0.2)
End With
Application.ScreenUpdating = True
End Sub
If you want to insert the new content before the existing content, simply change wdCollapseEnd to wdCollapseStart
 
Upvote 0
Solution
Thank you! The code works perfectly when there is no table in the document header. However, when a table is present, the code inserts the text into the table. I am unable to click anywhere else in the header apart from the table for some reason. I have tried recreating the document and adding a new table but I still cannot click a different area in the header. The table is small with two rows and two columns occupying about a quarter of the header. Is this something to do with the properties of the table?
 
Upvote 0
I found that if I split the table, I can get the cursor above it and the code works! Thank you.
 
Upvote 0
VBA Code:
Set HeaderRange = ActiveDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
With HeaderRange
    .Collapse wdCollapseEnd
    .InsertAfter ("Text 1" & vbTab & vbTab & "Text 2" & vbNewLine & "Text 3" & vbTab & vbTab & "Text 4")
    With .Font
        .Name = "Arial"
        .Size = 8
    End With
End With
Thank you, this also worked
 
Upvote 0
In which case:
VBA Code:
Sub HdrDemo()
Application.ScreenUpdating = False
With ActiveDocument.Sections.First
  With .Headers(wdHeaderFooterPrimary).Range
    If .Tables.Count > 0 Then
      If .Tables(1).Range.Start = .Start Then
        With .Tables(1)
          .Rows.Add BeforeRow:=.Rows(1)
          .Split BeforeRow:=.Rows(2)
        End With
        .Tables(1).Delete
      End If
    End If
    .Collapse wdCollapseStart
    .Text = "Text 1" & vbTab & vbTab & "Text 2" & vbCr & "Text 3" & vbTab & vbTab & "Text 4"
    .Font.Name = "Arial"
    .Font.Size = 8
  End With
  .PageSetup.HeaderDistance = CentimetersToPoints(0.2)
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That's brilliant, thank you. I think you're pretty awesome at this, I wondered if you could suggest any good websites/books to improve my VBA with Word in particular? I feel like my progress is really slow (I don't have a programming background though, maybe that's why).
 
Upvote 0
A web search for 'word vba tutorial', for example, turns up many possibilities. You could also visit sites like:

There are also boards that have forums specifically for Word VBA, such as:
Even without joining, you can often find useful code (and a wealth of other helpful material) on such boards.

As for not having "a programming background", nor do I.
 
Upvote 0

Forum statistics

Threads
1,223,573
Messages
6,173,133
Members
452,501
Latest member
musallam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top