Hi All,
I have a peculiar problem.
A user of my application uses a button to send a report by email. The report file (rtf) seems to be created, but the CONTENT of the file is from the previous time this same report had been run by the user.
In other words: The name of the RTF file correctly reflects the record Id, but the report shows the data of the previous record that was sent, with obviously a different recId.
Does anyone know what is going on here (and how to fix it)?
Here’s the relevant code:
Public Function SendMailMessage(stDocName As String, sTo As String, _ sOrderNumber As String, sSubject As String) As Boolean Dim sFileName As String Dim sFullFileName As String Dim sTempFolder As String sTempFolder = TempPath sFileName = sOrderNumber sFullFileName = sTempFolder & sFileName gsReportOrderNumber = sOrderNumber If stDocName Like "rpt*" Then sFullFileName = sFullFileName & ".rtf" DoCmd.OutputTo acOutputReport, stDocName, acFormatRTF, sFullFileName, False DoEvents SendMailMessage = True ElseIf stDocName Like "qry*" Then sFullFileName = sFullFileName & ".xls" DoCmd.OutputTo acOutputQuery, stDocName, acFormatXLS, sFullFileName, False DoEvents SendMailMessage = True Else MsgBox "Rapport type onbekend (geen qry of rpt). Verzenden email geannuleerd.", _ vbExclamation + vbOKOnly, GCSAPPNAME SendMailMessage = False Exit Function End If 'If report has been succesfully created then create email message If SendMailMessage Then If CreateMail(sTo, sSubject, " ", sFullFileName) Then SendMailMessage = True Else SendMailMessage = False End If End If On Error Resume Next Kill sFullFileName TidyUp: gsReportOrderNumber = "" Exit Function End Function Private Function CreateMail(sTo As String, sSubject As String, sBody As String, sAttachment As String) As Boolean Dim oMailItem As Object Dim oOLapp As Object On Error GoTo LocErr 'Fire up Outlook Set oOLapp = GetObject(, "Outlook.application") 'Open email object Set oMailItem = oOLapp.CreateItem(0) With oMailItem .To = sTo .Subject = sSubject .body = " " .attachments.Add sAttachment 'Display the message so user can edit and decide whether or not to send .Display Set oOLapp = Nothing Set oMailItem = Nothing End With CreateMail = True Exit Function TidyUp: CreateMail = False Exit Function LocErr: If Err.Number = 429 Then MsgBox "Outlook is niet gestart, s.v.p. Outlook starten en opnieuw proberen.", vbExclamation + vbOKOnly, "Outlook is niet gestart" Resume TidyUp End If MsgBox "Foutmelding tijdens maken email bericht. Foutboodschap:" & vbNewLine & _ Err.Description, vbOKOnly + vbExclamation, "Fout tijdens maken email" Resume TidyUp End Function