Question

Ok, so I've been putting band aids on top of band aids on a simple snippet of code to get an email to send. What I've been trying to do is get an email to send through outlook. My first issue was runtime object define 287 at the following line:

Set appOutlookRec = appOutlookMsg.Recipients.Add

so to counter that I added:

Set objNS = appOutlook.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)

And that stopped that. Now the email will form and I can use .display to see my email, but when I try and use .send the instance of Outlook closes before the email is actually sent.

To counter that I force outlook to open, but I would like to check if an instance is already open. To open the inbox I use:

Set appOutlook = CreateObject("Outlook.Application")
Set objNS = appOutlook.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
olFolder.Display

Am I over complicating the automation process? Or am I on the right path and someone can help checking if an instance of Outlook is open in the explorer window?

Thanks

Update

Const olMailItem = 0
Const olTo = 1
Const olCC = 2
Const olFolderInbox = 6
Dim appOutlook As Object
Dim appOutlookMsg As Object
Dim appOutlookRec As Object
Dim objNS As Object
Dim olFolder As Object

Set appOutlook = CreateObject("Outlook.Application")
Set objNS = appOutlook.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
olFolder.Display

'Create a new empty email message
Set appOutlookMsg = appOutlook.CreateItem(olMailItem)

With appOutlookMsg
Set appOutlookRec = appOutlookMsg.Recipients.Add("....@gmail.com")
    appOutlookRec.Type = olTo
    .Subject = "Testing Closed Tickets"
    .Body = "This is just a test."
    '.Display
    .Send
End With

Set appOutlookMsg = Nothing
Set appOutlook = Nothing
Set appOutlookRec = Nothing
Was it helpful?

Solution

I've encountered something similar before and I think usually you can avoid the error by saving the email prior to sending:

appOutlookMsg.Save
appOutlookMsg.Send
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top