populate word template from access

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.

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
 
B

babyatx13 via AccessMonster.com

With oApp.Selection.Find
.Text = "Address " 'or variable with text you want to
replace.
.Replacement.Text = Me.Address
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oApp.Selection.TypeText Text:=Str(variable) 'or
oApp.Selection.TypeText Text:=variable
oApp.Selection.MoveDown Unit:=wdLine, Count:=4

hope this helps
K Board
 
B

babyatx13 via AccessMonster.com

replace oApp with your wdApp
With oApp.Selection.Find
.Text = "Address " 'or variable with text you want to
replace.
.Replacement.Text = Me.Address
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oApp.Selection.TypeText Text:=Str(variable) 'or
oApp.Selection.TypeText Text:=variable
oApp.Selection.MoveDown Unit:=wdLine, Count:=4

hope this helps
K Board
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
[quoted text clipped - 41 lines]
Thanks in advance
 
J

JimBurke via AccessMonster.com

Here's code that I used successfully...

objWord.Documents.Add templateName 'templateName is the file name of the
template
With objWord.ActiveDocument.Bookmarks
.Item("PatientName").Range.Text = PatientName
.Item("PatientDOB").Range.Text = Nz(PatientDOB, vbNullString)
.Item("PatientDOS").Range.Text = Format(DOS, "mm/dd/yy")
End With
objWord.ActiveDocument.SaveAs newFileName
objWord.ActiveDocument.Close

'newFileName' is the name you want for the new document that wsa based on the
template.
 

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