# Word VBA - Add text to header without overwriting existing content



## CNorth (Apr 17, 2021)

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.



```
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


----------



## mart37 (Apr 17, 2021)

```
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 (Apr 17, 2021)

That's as simple as:

```
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


----------



## CNorth (Apr 17, 2021)

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 (Apr 17, 2021)

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


----------



## CNorth (Apr 17, 2021)

mart37 said:


> ```
> Set HeaderRange = ActiveDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
> With HeaderRange
> .Collapse wdCollapseEnd
> ...


----------



## Macropod (Apr 18, 2021)

In which case:

```
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 (Apr 18, 2021)

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 (Apr 18, 2021)

A web search for 'word vba tutorial', for example, turns up many possibilities. You could also visit sites like:


			Word MVP Site Home
		






						Microsoft Word FAQ - Frequently Asked Questions - Help with Microsoft Word
					

Frequently Asked Questions about Microsoft Word compiled  	from the Microsoft Word newsgroups.  In addition to answers to those Word questions,  	this FAQ contains extensive links to other Word resources.



					www.addbalance.com
				











						Graham Mayor - Word Pages
					

A collection of tips and add-ins to benefit Word users.



					www.gmayor.com
				








						Microsoft Word Help, Tips and Tutorials @ The Anchorage
					

An index with links to all Microsoft Word Help, Tips and Tuitorials @ The Anchorage.



					gregmaxey.com
				








						Microsoft Word Help, Tips and Tutorials – Making the most of Word in your business | ShaunaKelly.com
					

Microsoft Word. Help for beginners, users, developers and managers. Formatting, bullets, numbering, and track changes. By Microsoft Word MVP Shauna Kelly.




					shaunakelly.com
				




There are also boards that have forums specifically for Word VBA, such as:





						Word Programming / VBA / Macros
					

Post any general Word programming or macro questions here.



					www.excelforum.com
				








						Word VBA Forum
					

Word VBA programming, UserForms.



					www.msofficeforums.com
				





			Word Help
		

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.


----------

