Option Compare Database
Option Explicit
' You need to declare a reference to the Outlook library, and the filesystemobject.
'
' Look in the menu above, and click Tools, then select References
'
' Scroll down the list until you see
' Microsoft Scripting Runtime -- and put a check next to it (if one is not there already)
'
' Microsoft Outlook Object Library -- check that.
' There will be some version number there as well; it doesn't matter.
' This will work with Outlook98 and Outlook2000
Public Sub SendeMailAttachment()
Dim db As DAO.Database
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
'
Dim FileAttachment As String
'
'Trap to handle No Send virus check
On Error GoTo NoSend
'
'
Dim Response As String
'
'Write the e-mail body
DoCmd.OpenForm "F-emailbodytext", , , , , acDialog
'
'Now output the file
DoCmd.OutputTo acReport, "R-emailbodytext", "MS-DOSText(*.txt)", "c:\temp\emailbody.txt", False, "", 0
'
'
Set fso = New FileSystemObject
' First, we need to know the subject. We can't very well be sending around blank messages...
'We Need A Subject Line!")
Subjectline$ = "Your Quotation from Four Seasons"
'
'Default file name and path
BodyFile$ = "c:\temp\emailbody.txt"
'
If BodyFile$ = "" Then
MsgBox "No body, no message." & vbNewLine & vbNewLine & "Quitting...", vbCritical, "I Ain't Got No-Body!"
Exit Sub
End If
' Check to make sure the file exists...
If fso.FileExists(BodyFile$) = False Then
MsgBox "The body file isn't where you say it is. " & vbNewLine & vbNewLine & "Quitting...", vbCritical, "I Ain't Got No-Body!"
Exit Sub
End If
' Since we got a file, we can open it up.
Set MyBody = fso.OpenTextFile(BodyFile, ForReading, False, TristateUseDefault)
'and read it into a variable.
MyBodyText = MyBody.ReadAll
' and close the file.
MyBody.Close
' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application
' Set up the database and query connections
Set db = CurrentDb()
'
Set MyMail = MyOutlook.CreateItem(olMailItem)
' This addresses it
MyMail.To = strQuotationeMail
'This gives it a subject
MyMail.Subject = Subjectline$
'This gives it the body
MyMail.Body = MyBodyText
'
FileAttachment$ = "c:\temp\Quotation_" & strClientNumber & ".pdf"
'
'Now check that the file exists - if the file has been output, it will be!!
'
If fso.FileExists(FileAttachment$) = False Then
'
MsgBox "The Attachment file is not where you say it is. " & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "Incorrect file path"
Exit Sub
End If
'
MyMail.Attachments.Add FileAttachment$, olByValue, 1, "Quotation from Four Seasons"
'
'This sends it!
MyMail.Send
'Cleanup after ourselves
Cleanup:
Set MyMail = Nothing
'Uncomment the next line if you want Outlook to shut down when its done.
'... otherwise, it will stay running.
'MyOutlook.Quit
Set MyOutlook = Nothing
db.Close
Set db = Nothing
'
Exit Sub
'Trap to handle No Send dialogue
NoSend:
'
If Err.Number = 287 Then
MsgBox "Send to Outbox Stopped", vbCritical, "Send Stopped"
GoTo Cleanup
'
Else
MsgBox "Send or Programme Error " & Err.Number & vbLf & Err.Description, vbCritical, "Send Stopped"
GoTo Cleanup
End If
'
End Sub