Consulting

Results 1 to 6 of 6

Thread: How to change words in Header/Footer from Excel

  1. #1
    VBAX Regular
    Joined
    Jun 2008
    Location
    California
    Posts
    89
    Location

    Red face How to change words in Header/Footer from Excel

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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 Sub
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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]

  4. #4
    VBAX Regular
    Joined
    Jun 2008
    Location
    California
    Posts
    89
    Location
    Thanks! I will give it a try...

  5. #5
    VBAX Regular
    Joined
    Jun 2008
    Location
    California
    Posts
    89
    Location
    .

  6. #6
    VBAX Regular
    Joined
    Jun 2008
    Location
    California
    Posts
    89
    Location
    [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]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •