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
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