Access/Outlook 2010
I used the code listed at the bottom to email a report as an attachment.
It used to work fine but now it gives me an error:
#287 Application-defined or Object-defined error
Caused by these lines:
Any idea how to fix this?
Update: Outlook has to be opened.
CreateObject was not sufficient for this. When I added this to my code, it fixed the problem: Shell "Outlook.exe"
Not a nice solution, is there a way to get around it?
Thanks
I used the code listed at the bottom to email a report as an attachment.
It used to work fine but now it gives me an error:
#287 Application-defined or Object-defined error
Caused by these lines:
Code:
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Any idea how to fix this?
Update: Outlook has to be opened.
CreateObject was not sufficient for this. When I added this to my code, it fixed the problem: Shell "Outlook.exe"
Not a nice solution, is there a way to get around it?
Thanks
Code:
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
Dim Currfile As String
Currfile = "D:\Archive\Test.pdf"
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("myemail1@gmail.com")
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("myemail2@gmail.com")
objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = "Test Report"
.Body = "Here are the reports" & vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance
' Add attachments to the message.
.Attachments.Add Currfile
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
.Send
End With
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Last edited: