PDA

View Full Version : How to change words in Header/Footer from Excel



jazzyt2u
07-25-2008, 10:15 AM
I will need to change certain words in the header and footer in Word but need the code to be in Excel.

Also can I place a Heading in the Header to be used in the TOC? If so how do I show that it's a heading?

Thanks

macropod
07-27-2008, 07:59 PM
Hi Jazzyt2u,

You could use something like:
Sub DocModify()
Dim wdApp As Word.Application
Dim WdDoc As String
Dim WdHdr As String
Dim WdFtr As String
'Copy ranges
WdHdr = ActiveWorkbook.Sheets(1).Range("A1").Value
WdFtr = ActiveWorkbook.Sheets(1).Range("B1").Value
'Establish link to Word
WdDoc = "C:\My Documents\MyFile.doc"
If Dir(WdDoc) <> "" Then
Set wdApp = New Word.Application
wdApp.Visible = True
With wdApp
'open the Word Document
Documents.Open Filename:=WdDoc
With ActiveDocument
' The rest of your code header/footer update code goes here
End With
End With
Else
MsgBox "File: " & WdDoc & " not found."
End If
Set wdApp = Nothing
End SubYou'll need to add a reference to MS Word in the VBE. Depending on the nature of the document you're working on and what you're trying to do, you may need to test for the existence of the particular header or footer and, perhaps, use Find/replace or Delete/TypeText.

macropod
07-27-2008, 08:01 PM
As for the heading, have you considered putting the heading in the body of the document and using a STYLEREF field to replicate it in the header?

jazzyt2u
07-28-2008, 09:24 AM
Thanks! I will give it a try...

jazzyt2u
10-07-2008, 04:18 PM
.

jazzyt2u
10-07-2008, 04:21 PM
oDoc.ActiveWindow.ActivePane.View.SeekView = 9 'This opens the header on the current page


With oWord.Selection.Find


.ClearFormatting
.Replacement.ClearFormatting
.Text = "ReportName"
.Replacement.Text = MyRpt
.Forward = True


.Execute Replace:=1
End With



oDoc.ActiveWindow.ActivePane.View.SeekView = 0 'This closes the header