rankhornjp
07-12-2011, 10:16 AM
I want to use the information in my Access 2007 database to fill in different Word 2007 forms depending on which button I press.
Here is the code I have. (It works for one button)
Private Sub btnPrintDUI_Click()
'Open up form and fill it in with information from database.
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("c:\users\samsclub\desktop\DUIContractSignUP.docm", , True)
With doc
.FormFields("firstName").Result = Me!pFname
.FormFields("lastName").Result = Me!pLname
.FormFields("middleName").Result = Me!pMi
.FormFields("streetAddress").Result = Me!pAddress1
.FormFields("cityAddress").Result = Me!pCity
.FormFields("stateAddress").Result = Me!pState
.FormFields("zipAddress").Result = Me!pZip
.FormFields("numberHome").Result = Me!pPhone1
.FormFields("numberCell").Result = Me!pPhone2
.FormFields("SSN").Result = Me!pSSN
.FormFields("dateBirth").Result = Me!pDOB
.FormFields("licenseNumber").Result = Me!pDLNumber
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
I need to change the file that opens depending on which button I press. I know that I can paste this code into each buttons "On Click", but I was hoping there was a better way of doing it so I wouldn't have the same code posted 7-8 times in the same project.
I've tried searching for an answer, but my search skills aren't turning up anything.
Thanks
James
ETA: I have to double click the button, if the Word file isn't already open. (dont know why)
Here is the code I have. (It works for one button)
Private Sub btnPrintDUI_Click()
'Open up form and fill it in with information from database.
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("c:\users\samsclub\desktop\DUIContractSignUP.docm", , True)
With doc
.FormFields("firstName").Result = Me!pFname
.FormFields("lastName").Result = Me!pLname
.FormFields("middleName").Result = Me!pMi
.FormFields("streetAddress").Result = Me!pAddress1
.FormFields("cityAddress").Result = Me!pCity
.FormFields("stateAddress").Result = Me!pState
.FormFields("zipAddress").Result = Me!pZip
.FormFields("numberHome").Result = Me!pPhone1
.FormFields("numberCell").Result = Me!pPhone2
.FormFields("SSN").Result = Me!pSSN
.FormFields("dateBirth").Result = Me!pDOB
.FormFields("licenseNumber").Result = Me!pDLNumber
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
I need to change the file that opens depending on which button I press. I know that I can paste this code into each buttons "On Click", but I was hoping there was a better way of doing it so I wouldn't have the same code posted 7-8 times in the same project.
I've tried searching for an answer, but my search skills aren't turning up anything.
Thanks
James
ETA: I have to double click the button, if the Word file isn't already open. (dont know why)