This is the code that I am using.
Like I said, it works in Win 98 & 2000, so I don't want to change it too much.
Public Sub EmailSociety(ToAddress As String, FromAddress As String, Subject As String, BodyText As String)
'Package these files.
Dim strFilename As String
Dim strFilesRemaining As String
Dim intDotPos As Integer
Dim intSpacePos As Integer
Dim recFrontPage As Recordset
Dim recRegionPrintOptions As Recordset
Dim intCatCode As Integer
Dim intStoreCount As Integer
Dim strError As String
Dim lngStoreID As Long
Dim objDistiller As ACRODISTXLib.PdfDistiller
Dim colAttachments As Collection
Dim varFilename As Variant
Dim lngError As Long
Dim varRecipient As Variant
Dim colRecipients As Collection
On Error GoTo Err
DoCmd.Hourglass True
Set recFrontPage = CurrentDb.OpenRecordset("SELECT DISTINCT PLANCODE, CATEGORYNAME, FormattedNOMINALMETERAGE FROM qmakzttblPrintPlans", DB_OPEN_SNAPSHOT)
'Set recRegionPrintOptions = CurrentDb.OpenRecordset("tblRegionPrintSettings", dbOpenSnapshot)
DoCmd.Hourglass False
Set colAttachments = New Collection
If Not recFrontPage.EOF Then
recFrontPage.MoveLast ' There's an overhead on this, but the user wants to display progress, so need RecordCount.....
recFrontPage.MoveFirst
'do something to indicate progress....
Forms("Select Store Report").SetProgressMax recFrontPage.RecordCount
Forms("Select Store Report").SetProgressValue 0
Forms("Select Store Report").ShowProgress
End If
Set objDistiller = New ACRODISTXLib.PdfDistiller
lngStoreID = -1
intCatCode = -1
intStoreCount = 0
m_strPDFDir = Application.CurrentProject.Path & "\PDF\"
' Check for an appropriate subdirectory to use, if there isn't one, then create it
If Dir(m_strPDFDir, vbDirectory) = "" Then
MkDir m_strPDFDir
Else
On Error Resume Next
Kill m_strPDFDir & "*.pdf"
On Error GoTo Err
End If
Do While Not recFrontPage.EOF
DoEvents
' PRN
' strFilename = Dir(m_strPDFDir & recFrontPage("PLANCODE").Value & "_prn.pdf")
strFilename = Dir(m_strPDFDir & recFrontPage("CATEGORYNAME").Value & "_" _
& recFrontPage("FormattedNOMINALMETERAGE").Value & "_prn.pdf")
If strFilename <> "" Then
colAttachments.Add m_strPDFDir & strFilename
Else
' strFilename = m_strPDFDir & recFrontPage("PLANCODE").Value & "_prn.pdf"
strFilename = m_strPDFDir & recFrontPage("CATEGORYNAME").Value & "_" _
& recFrontPage("FormattedNOMINALMETERAGE").Value & "_prn.pdf"
If Dir(g_conPlanFilePath & recFrontPage("PLANCODE").Value & ".prn") <> "" Then
objDistiller.FileToPDF g_conPlanFilePath & recFrontPage("PLANCODE").Value & ".prn", strFilename, ""
If Dir(strFilename) <> "" Then
colAttachments.Add strFilename
End If
End If
End If
' ML
' strFilename = Dir(m_strPDFDir & recFrontPage("PLANCODE").Value & "_ml.pdf")
strFilename = Dir(m_strPDFDir & recFrontPage("CATEGORYNAME").Value & "_" _
& recFrontPage("FormattedNOMINALMETERAGE").Value & "_ml.pdf")
If strFilename <> "" Then
colAttachments.Add m_strPDFDir & strFilename
Else
' strFilename = m_strPDFDir & recFrontPage("PLANCODE").Value & "_ml.pdf"
strFilename = m_strPDFDir & recFrontPage("CATEGORYNAME").Value & "_" _
& recFrontPage("FormattedNOMINALMETERAGE").Value & "_ml.pdf"
If Dir(g_conPlanFilePath & recFrontPage("PLANCODE").Value & ".ml") <> "" Then
objDistiller.FileToPDF g_conPlanFilePath & recFrontPage("PLANCODE").Value & ".ml", strFilename, ""
If Dir(strFilename) <> "" Then
colAttachments.Add strFilename
End If
End If
End If
' FL
' strFilename = Dir(m_strPDFDir & recFrontPage("PLANCODE").Value & "_fix.pdf")
strFilename = Dir(m_strPDFDir & recFrontPage("CATEGORYNAME").Value & "_" _
& recFrontPage("FormattedNOMINALMETERAGE").Value & "_fix.pdf")
If strFilename <> "" Then
colAttachments.Add m_strPDFDir & strFilename
Else
' strFilename = m_strPDFDir & recFrontPage("PLANCODE").Value & "_fix.pdf"
strFilename = m_strPDFDir & recFrontPage("CATEGORYNAME").Value & "_" _
& recFrontPage("FormattedNOMINALMETERAGE").Value & "_fix.pdf"
If Dir(g_conPlanFilePath & recFrontPage("PLANCODE").Value & ".fix") <> "" Then
objDistiller.FileToPDF g_conPlanFilePath & recFrontPage("PLANCODE").Value & ".fix", strFilename, ""
If Dir(strFilename) <> "" Then
colAttachments.Add strFilename
End If
End If
End If
RetryNext: ' Label for gotos in Err block.
On Error GoTo Err
recFrontPage.MoveNext
Forms("Select Store Report").ProgressStepIt
Loop
Set objDistiller = Nothing
Connect cHostIP_Society, cUserID_Society, lngError
' Send an email to the entered addresses with the appropriate attachments
CreateEmail FromAddress, Subject, BodyText, lngError
'<<< 12-Jan-2004, CSS
'AddRecipient ToAddress
For Each varRecipient In colRecipients
AddRecipient varRecipient
Next varRecipient
For Each varFilename In colAttachments
AddAttachment varFilename, lngError
Next varFilename
Set colRecipients = GetRecipients(ToAddress)
'<<< CSS end
Send lngError
Disconnect
' Tidy up
Forms("Select Store Report").ShowProgress IsShown:=False
Forms("Select Store Report").Refresh
recFrontPage.Close
Set objDistiller = Nothing
DoCmd.SetWarnings True
Exit Sub
Err:
strError = "An error has occured during the creation of this email."
strError = strError + vbCrLf + "Error Number: " + str$(Err.Number) + vbCrLf + Err.Description
strError = strError + vbCrLf + "If this error has been rectified, then Retry the job."
Select Case MsgBox(strError, vbAbortRetryIgnore, "Print Error")
Case vbRetry
' Retry the error line
On Error GoTo Err
Resume
Case vbIgnore
' Try the next one
Err.Clear
Resume RetryNext
Case vbAbort
' Display the error
RaiseError "basEPlans.EmailSociety"
End Select
' Tidy up, and exit.
DoCmd.SetWarnings True
Set objDistiller = Nothing
Disconnect
Forms("Select Store Report").ShowProgress IsShown:=False
Forms("Select Store Report").Refresh
If Not recFrontPage Is Nothing Then recFrontPage.Close
End Sub
'<<< 12-Jan-2004, CSS
Private Function GetRecipients(ToAddress As String) As Collection
Dim strAddresses As String
Dim colRecipients As Collection
Dim lngPosition As Long
Dim strRecipient As String
On Error Resume Next
Set colRecipients = New Collection
' Tidy up the ToAddress string
strAddresses = Replace(ToAddress, vbCr, "")
strAddresses = Replace(strAddresses, vbLf, "")
' Look for a ; in the data
lngPosition = InStr(strAddresses, ";")
While lngPosition > 0
strRecipient = Left(strAddresses, lngPosition - 1)
colRecipients.Add strRecipient
strAddresses = Mid(strAddresses, lngPosition + 1)
lngPosition = InStr(strAddresses, ";")
Wend
If strAddresses <> "" Then colRecipients.Add strAddresses
Set GetRecipients = colRecipients
End Function
'<<< CSS end