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
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
Hi Jazzyt2u,
You could use something like:You'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.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 Sub
Cheers
Paul Edstein
[Fmr MS MVP - Word]
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?
Cheers
Paul Edstein
[Fmr MS MVP - Word]
Thanks! I will give it a try...
.
[vba]
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
[/vba]