PDA

View Full Version : Solved: Several buttons doing the same thing to different word files.



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)

rankhornjp
07-12-2011, 07:46 PM
Partial Fix:


'Declare global variable
Dim btn as String
___________________________________________________________

Private Sub printForm()

'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

'Determine which button was pressed
If btn = 1 then
Set doc = appWord.Documents.Open("c:\users\samsclub\desktop\DUIContractSignUP.docm", , True)
End If

If btn = 2 Then
Set doc = appWord.Documents.Open("c:\users\samsclub\desktop\File 2.doc", , True)
End If

If btn = 3 Then
Set doc = appWord.Documents.Open("c:\users\samsclub\desktop\File 3.doc", , True)
End If

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
__________________________________________________________
'Do this for each button, changing the number
Private Sub btnPrintDUI_Click()

btn = 1

Call printForm

End Sub


I still have to double click the button, if the Word file isn't already open. (dont know why). But now it works for all buttons without having the code pasted a bunch of times