Replace text in Word header from Microsoft Access VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • misternaux
    New Member
    • May 2013
    • 1

    Replace text in Word header from Microsoft Access VBA

    Hi all,

    I would like to replace text in a Word template header, which is opened from MS Access.

    I can open the file just fine...:

    Code:
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
    Set doc = oApp.Documents.Add(Template:="C:\CL_Template.dotx", NewTemplate:=False, DocumentType:=0)
    I can also access many properties of Word such as oApp.Selection. TypeText, but I cannot figure out how to replace text in the header of my word document with a variable declared in Access.

    I know in Word, when recording the macro, it looks like this, but I can't get it to work in Access:

    Code:
    Dim myStoryRange As Range
    For Each myStoryRange In ActiveDocument.StoryRanges
        With myStoryRange.Find
            .Text = "Letterdate"
            .Replacement.Text = let_date_var
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
    Next myStoryRange
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32636

    #2
    Try this :
    Code:
    Private Const wdFindContinue As Long = 1
    Private Const wdReplaceAll As Long = 2
    
    Dim oApp As Word.Application
    Dim doc As Word.Document
    Dim myStoryRange As Word.Range
    
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
    Set doc = oApp.Documents.Add(Template:="C:\CL_Template.dotx", NewTemplate:=False, DocumentType:=0)
    
    For Each myStoryRange In doc.StoryRanges
        With myStoryRange.Find
            .Text = "Letterdate"
            .Replacement.Text = let_date_var
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
    Next myStoryRange
    It's air-code, so it's just something to start you off with. Good luck.

    PS. Application Automation may be of some help.

    Comment

    Working...