populate word template from access form

S

Sam

Hi All, I am trying to populate word template with current record in access.
Here is what I have so far. But it doesnt work. I have created a Button
"Export to Work" on Access Form which is connected to the database. Here is
the code for the button

Private Sub ExportWord_Click()

Dim wdApp As Object
Dim doc As Object
On Error Resume Next
Set wdApp = GetObject("C:\My Documents\Address.dotx", "Word.Application")

If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Dim FName As String

sWdFileName = Application.GetOpenFilename(, , , , False)
'Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

On Error Resume Next
Set doc = wdApp.Documents.Open(sWdFileName)

With wdApp
.Variables("Address").Result = Me.Address
.Variables("City").Result = Me.City
.Variables("State").Result = Me.State
End With
wdApp.ActiveDocument.Fields.Update

FName = "C:\My Documents\" _
& "Address" & ".doc"

wdApp.ActiveDocument.SaveAs FileName:=FName

wdApp.Visible = True

Set doc = Nothing
Set wdApp = Nothing
wApp.Quit

End Sub

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top