Hello all,
I am trying to populate a word document from a listbox selection.
The word docx populates just fine with a form (Rowsource is a query) and the code below in the click event of the button.
What I would like to happen, is when the user selects a record from the listbox and then clicks the "Generate word document" button this selection will populate my word doc.
The code was found doing a search (maybe microsoft, not sure):
I tried to include my listbox, but I get the error code 3061-too few parameters.
When I remove the listbox code, then the form opens blank
Any help would be appreciated.
I am trying to populate a word document from a listbox selection.
The word docx populates just fine with a form (Rowsource is a query) and the code below in the click event of the button.
What I would like to happen, is when the user selects a record from the listbox and then clicks the "Generate word document" button this selection will populate my word doc.
The code was found doing a search (maybe microsoft, not sure):
Code:
Option Compare Database
Option Explicit
Const DOC_PATH As String = "C:\Users\arrudad\Documents\AutoForms\"
Const DOC_NAME As String = _
"e60 Day Procedure-autov2.docm"
Private Sub cmdPrintForm_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strCriteria As String
On Error Resume Next
Set appWord = GetObject(, "Word.application")
If Err = 429 Then
Set appWord = New Word.Application
Err = 0
End If
With appWord
Set doc = .Documents(DOC_NAME)
If Err = 0 Then
' If MsgBox("Do you want to save the current document " _
' & "before updating the data?", vbYesNo) = vbYes Then
' .Dialogs(wdDialogFileSaveAs).Show
' End If
doc.Close False
End If
On Error GoTo ErrorHandler
Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
If Me.lstCaseNumbers.ItemsSelected.Count > 0 Then
strCriteria = "[CaseNumber]='" & Me![lstCaseNumbers].Column(0) & "'"
Set rst = CurrentDb.OpenRecordset("Q60DayForm")
If Not rst.EOF Then
strCriteria = Nz(rst.Fields(0).Value)
rst.Close
End If
End If
With doc
.FormFields("fldFirstName").Result = Nz(Me![First Name] & "" & "," & " " & "" & [Last Name])
.FormFields("fldAddress").Result = Nz(Me![Street Address])
.FormFields("fldCity").Result = Nz(Me![City] & "," & " " & [State] & "," & " " & [Zip Code])
.FormFields("fldFirstName1").Result = Nz(Me![First Name] & "" & "," & " " & "" & [Last Name])
.FormFields("fldCallInDate").Result = Nz(Me![Call_In Date])
.FormFields("fld60DayDate").Result = Nz(Me![60DayDate])
.FormFields("fldCaseNumber").Result = Nz(Me!CaseNumber)
.FormFields("fldIncidentDate").Result = Nz(Me![Incident Date])
.FormFields("fldInvestigator").Result = Nz(Me![Case Investigator])
End With
.Visible = True
.Activate
End With
Set rst = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub
ErrorHandler:
MsgBox Err & Err.Description
'End If
End Sub
I tried to include my listbox, but I get the error code 3061-too few parameters.
When I remove the listbox code, then the form opens blank
Any help would be appreciated.