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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,091
Office Version
  1. 2016
Platform
  1. Windows
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
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,544
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
 
Solution

CNorth

New Member
Joined
Jan 7, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
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?
 

CNorth

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

ADVERTISEMENT

I found that if I split the table, I can get the cursor above it and the code works! Thank you.
 

CNorth

New Member
Joined
Jan 7, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
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
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,544

ADVERTISEMENT

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
 

CNorth

New Member
Joined
Jan 7, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
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).
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,544
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,479
Messages
5,636,574
Members
416,925
Latest member
malamutus

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
Top