Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit Header in word using code

Status
Not open for further replies.

hsviljoen

Programmer
May 26, 2004
73
ZA
Hi there,
I need to change the header of a document based on a selection from a dropdown in page1 of the document - On thing that makes it a bit tricky is that the footer needs to be the same thru out the document ( incl page1) but the header should not display in the first page??? Your help would by much appreciated.

Thanks
 
You need to write the header and footer for the rest of the document first and then set the

'File / Page Setup / Layout / Different first page' check box.

(The VBA for which is
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True)

Then go back and write the header and footer for the first page.

Note, your document will have to be more than one page long to do this.
 
This wont work unfortunately - The template is set as one page - however when the user completes it will go onto several pages - All I need is someway to hide the header on the first page. Is it possible to set the height of the header to 0???? Only for the first header though.....
 
As far as I know any settings on the header would be carried over to all following pages.

Could you not trap the save commands, run a piece of code to check the number of pages and re-write the header and footer accordingly?

e.g.

Sub FileSave()
'On save check number of pages
If ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)= 1 Then
'Run code to write header for first page
Else
'Run code to write header for all pages
'Switch first page to be different
'Run code to write header for first page
End If

You would probably also do this for FileSaveAs() and maybe FileClose() to stop users getting round it.



 
Ive got it to work!!! I added a table to the header - Also when I created the header i selected the option "different first page". SO: Ive created the header in the second page and afterwards just deleted the second page.... Here is my code , the variables are populated with values from embedded dropdown on the first page:


Thank you for your help VBAWillow !

------- CODE -------

Sub Headerfunctionnew()
Dim SecCount As Integer
Dim CompLine1 As String
Dim CompLine2 As String
Dim AddrLine1 As String
Dim AddrLine2 As String
Dim AddrLine3 As String
Dim FooterLine1 As String
Dim FooterLine2 As String

CompLine1 = ddcompany.Text
CompLine2 = ActiveDocument.Tables(1).Cell(2, 1).Range.Text
AddrLine1 = ddaddress.Text

If lstLines2and3.ListCount > 1 Then
AddrLine2 = lstLines2and3.List(0)
End If

If lstLines2and3.ListCount = 2 Then
AddrLine3 = lstLines2and3.List(1)
End If

If LstFooters.ListCount > 1 Then
FooterLine1 = LstFooters.List(0)
End If

If LstFooters.ListCount = 2 Then
FooterLine2 = LstFooters.List(1)
End If

ActiveDocument.Sections(SecCount).Headers(1).Range.Tables(1).Cell(1, 1).Range.Text = CompLine1
ActiveDocument.Sections(SecCount).Headers(1).Range.Tables(1).Cell(2, 1).Range.Text = CompLine2
ActiveDocument.Sections(SecCount).Headers(1).Range.Tables(1).Cell(1, 2).Range.Text = AddrLine1
ActiveDocument.Sections(SecCount).Headers(1).Range.Tables(1).Cell(2, 2).Range.Text = AddrLine2
ActiveDocument.Sections(SecCount).Headers(1).Range.Tables(1).Cell(3, 2).Range.Text = AddrLine3

ActiveDocument.Sections(SecCount).Footers(1).Range.Tables(1).Cell(1, 1).Range.Text = FooterLine1
ActiveDocument.Sections(SecCount).Footers(1).Range.Tables(1).Cell(2, 1).Range.Text = FooterLine2

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top