Code to fill Word form for multiple records from Access

  • Thread starter Thread starter rollininclover
  • Start date Start date
R

rollininclover

This is the code I've been working with, but it freezes Access:

Private Sub Command66_Click()
'Print Physician Profile.
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 rs = Db.OpenRecordset
rst.Open Me.RecordSource, CurrentProject.Connection
'Cycle through records to fill Word form fields
Do While Not rst.EOF
Set doc = appWord.Documents.Open("Z:\My Documents\ACCESS\Access to
Word.doc", , True)
With doc
..FormFields("fldMnemonic").Result = rst!Mnemonic
..FormFields("fldCMAMIPct").Result = rst!CMAMIPct
..FormFields("fldCMCHFPct").Result = rst!CMCHFPct
..FormFields("fldCMPNEPct").Result = rst!CMPNEPct
..FormFields("fldCMSCIPPct").Result = rst!CMSCIPPct
..FormFields("fldCMAMIOPPct").Result = rst!CMAMIOPPct
..FormFields("fldCMSURGOPPct").Result = rst!CMSURGOPPct
..FormFields("fldCMAMINum").Result = rst!CMAMINum
..FormFields("fldCMCHFNum").Result = rst!CMCHFNum
..FormFields("fldCMPNENum").Result = rst!CMPNENum
..FormFields("fldCMSCIPNum").Result = rst!CMSCIPNum
..FormFields("fldCMAMIOPNum").Result = rst!CMAMIOPNum
..FormFields("fldCMSURGOPNum").Result = rst!CMSURGOPNum
..FormFields("fldCMAMIDen").Result = rst!CMAMIDen
..FormFields("fldCMCHFDen").Result = rst!CMCHFDen
..FormFields("fldCMPNEDen").Result = rst!CMPNEDen
..FormFields("fldCMSCIPDen").Result = rst!CMSCIPDen
..FormFields("fldCMAMIOPDen").Result = rst!CMAMIOPDen
..FormFields("fldCMSURGOPDen").Result = rst!CMSURGOPDen
..Visible = True
..Activate
..SaveAs "4Q 09 " & Me!Mnemonic
rst.MoveNext
End With
Loop
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
 
Hi, John, thanks for the help. I changed rs to rst, but Access only produces
one Word form and then locks up. Any other ideas?

Thanks, again, for your help.
 
Back
Top