tca_VB
09-12-2008, 01:21 PM
I'm pretty close and hoping someone can help me.
I would like to click a command button from a form. The command will go to the first row of a query which is filtered by parameter [SelectedPIN] based on PIN_ID from active record on form.
The rest of the command should use the values from the first row to open a word document, update fields based on active record on form, then save and close word document.
Hope you followed that... The error message I get back is "Too Few Parameters. Expected 1"
Thanks in advance!
Private Sub cmdOpenWordDoc_Click()
On Error GoTo Err_cmdOpenWordDoc_Click
Dim oApp As Object
Dim dotString As String
Dim pathString As String
Dim saveString As String
Dim docString As String
Dim db As DAO.Database
Dim nameString As String
'Dim qdfDocs As DAO.QueryDef, might need this
Dim rstUnfiltered As DAO.Recordset
Dim rstFiltered As DAO.Recordset
Set db = CurrentDb()
Set rstUnfiltered = db.OpenRecordset("qryDocstoComplete")
Set rstFiltered = rstUnfiltered.OpenRecordset
rstFiltered.Filter = "[SelectedPIN]=" & Me.PIN_ID
'Go to First Record to Open Document
rstFiltered.MoveFirst
dotString = rstFiltered![Template_Name]
pathString = rstFiltered![Storage_Location] & "\"
Const conTEMPLATE_NAME = "pathString" & "dotString"
'Open Word Instance
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
'Open Template as Document
oApp.Documents.Add Template:=(conTEMPLATE_NAME)
'Code to populate Word Fields
With oApp.ActiveDocument 'need reference to current open word doc or oApp.Selection
.FormFields("crtsPatientName").Result = (Me!Client_Name)
.FormFields("crtsPatientSign").Result = (Me!Client_PIN)
.FormFields("crtsPatientSignDt").Result = Now()
.FormFields("crtsCareNameDt").Result = Now()
End With
' Save the document.
saveString = "Category_Storage" & "\"
docString = rstFiltered![Document_Name]
nameString = rstFiltered![Client_Name]
oApp.ActiveDocument.SaveAs FileName:=saveString & nameString & "_" & docString, FileFormat:=wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False
oApp.ActiveDocument.Close
' Quit Word.
oApp.Quit
' Clear the variable from memory.
Set oApp = Nothing
' Clear other variables from memory.
' Not sure that I have these right below
rstUnfiltered.Close
rstFiltered.Close
Set rstUnfiltered = Nothing
Set rstFiltered = Nothing
Set db = Nothing
Exit_cmdOpenWordDoc_Click:
Exit Sub
Err_cmdOpenWordDoc_Click:
MsgBox Err.Description
Resume Exit_cmdOpenWordDoc_Click
End Sub
I would like to click a command button from a form. The command will go to the first row of a query which is filtered by parameter [SelectedPIN] based on PIN_ID from active record on form.
The rest of the command should use the values from the first row to open a word document, update fields based on active record on form, then save and close word document.
Hope you followed that... The error message I get back is "Too Few Parameters. Expected 1"
Thanks in advance!
Private Sub cmdOpenWordDoc_Click()
On Error GoTo Err_cmdOpenWordDoc_Click
Dim oApp As Object
Dim dotString As String
Dim pathString As String
Dim saveString As String
Dim docString As String
Dim db As DAO.Database
Dim nameString As String
'Dim qdfDocs As DAO.QueryDef, might need this
Dim rstUnfiltered As DAO.Recordset
Dim rstFiltered As DAO.Recordset
Set db = CurrentDb()
Set rstUnfiltered = db.OpenRecordset("qryDocstoComplete")
Set rstFiltered = rstUnfiltered.OpenRecordset
rstFiltered.Filter = "[SelectedPIN]=" & Me.PIN_ID
'Go to First Record to Open Document
rstFiltered.MoveFirst
dotString = rstFiltered![Template_Name]
pathString = rstFiltered![Storage_Location] & "\"
Const conTEMPLATE_NAME = "pathString" & "dotString"
'Open Word Instance
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
'Open Template as Document
oApp.Documents.Add Template:=(conTEMPLATE_NAME)
'Code to populate Word Fields
With oApp.ActiveDocument 'need reference to current open word doc or oApp.Selection
.FormFields("crtsPatientName").Result = (Me!Client_Name)
.FormFields("crtsPatientSign").Result = (Me!Client_PIN)
.FormFields("crtsPatientSignDt").Result = Now()
.FormFields("crtsCareNameDt").Result = Now()
End With
' Save the document.
saveString = "Category_Storage" & "\"
docString = rstFiltered![Document_Name]
nameString = rstFiltered![Client_Name]
oApp.ActiveDocument.SaveAs FileName:=saveString & nameString & "_" & docString, FileFormat:=wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False
oApp.ActiveDocument.Close
' Quit Word.
oApp.Quit
' Clear the variable from memory.
Set oApp = Nothing
' Clear other variables from memory.
' Not sure that I have these right below
rstUnfiltered.Close
rstFiltered.Close
Set rstUnfiltered = Nothing
Set rstFiltered = Nothing
Set db = Nothing
Exit_cmdOpenWordDoc_Click:
Exit Sub
Err_cmdOpenWordDoc_Click:
MsgBox Err.Description
Resume Exit_cmdOpenWordDoc_Click
End Sub