What's so hard about
1) Declare the variables as Object, rather than Excel.Application, etc.
2) Use CreateObject to instantiate the objects, rather than the New keyword.
3) EIther replace any intrinsic constants with their values or else define
the same constants in your code.
?
Sub SendMessageGenerateReport(Optional AttachmentPath)
' Rule 1: Declare as Object
' Dim objOutlook As Outlook.Application
' Dim objOutlookMsg As Outlook.MailItem
' Dim objOutlookRecip As Outlook.Recipient
' Dim objOutlookAttach As Outlook.Attachment
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
Dim db As Database
Dim rsEmail As Recordset
Dim stTo, stFrom, stCC, stBCC, stSubject, stSal, stText As String
Dim stQuery As String
Dim stAttachPath As String
Dim stAccessLink As String
stSubject = "CA#: " & Forms![frmCorrectiveActionByPass]![CA#]
stText = "A Corrective Action request has been issued to you: " &
Forms![frmCorrectiveActionByPass]![CA#]
' Okay, you're already following rule 2 (use CreateObject rather than New)
'Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
'Create the message.
' Rule 3: either replace the constants
' (you're using olMailItem & olImportanceNormal)
' by their values (0 & 1 respectively), or else declare variables
' by those names.
Const olMailItem As Long = 0
Const olImportanceNormal As Long = 1
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = Forms![frmCorrectiveActionByPass]![Employee].Column(5)
.Subject = stSubject
.Body = stText
.Importance = olImportanceNormal ' Normal Importance
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub
Incidentally, your line
Dim stTo, stFrom, stCC, stBCC, stSubject, stSal, stText As String
doesn't do what you likely think it does. It actually only declares one
variable as a string: stText. All the others are actually being declared as
variants. To declare them all as strings, you need
Dim stTo As String, stFrom As String, stCC As String, stBCC As String,
stSubject As String, stSal As String, stText As String
--
Doug Steele, Microsoft Access MVP
(no private e-mails, please)
Secret Squirrel said:
Here is an example of what I'm using for email code. Can you give me an
example of how I would add theis late bound code to it?
Sub SendMessageGenerateReport(Optional AttachmentPath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim db As Database
Dim rsEmail As Recordset
Dim stTo, stFrom, stCC, stBCC, stSubject, stSal, stText As String
Dim stQuery As String
Dim stAttachPath As String
Dim stAccessLink As String
stSubject = "CA#: " & Forms![frmCorrectiveActionByPass]![CA#]
stText = "A Corrective Action request has been issued to you: " &
Forms![frmCorrectiveActionByPass]![CA#]
'Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
'Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = Forms![frmCorrectiveActionByPass]![Employee].Column(5)
.Subject = stSubject
.Body = stText
.Importance = olImportanceNormal ' Normal Importance
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub
Douglas J. Steele said:
You replace whatever existing early bound code you have with the
equivalent
late bound code everywhere in your application.