0

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
Grant
  • 903
  • 1
  • 16
  • 24
  • 2
    Did you try `.Save` the email before `.Send`? – David Zemens Oct 08 '13 at 19:30
  • 1
    Good (but strange) news! Outlook is the only Office app where `= CreateObject("Outlook.Application")` will create an instance if it does not exist **OR** use the existing instance, if Outlook is already running. – tbur Oct 08 '13 at 19:35
  • Can you post your entire code please? Are you running this code from Outlook or another source (I'm assuming MS Access)? You can make life a lot easier by referencing the Outlook runtime library in Access – enderland Oct 08 '13 at 19:35
  • 1
    I was going to mention that @tbur I thought I read that elsewhere but was trying to verify :) This is also true for PowerPoint. – David Zemens Oct 08 '13 at 19:37
  • @DavidZemens that worked perfectly. Now I don't have to use `olFolder.Display` to open the instance in explorer. If you would like to actually write that as an answer I will gladly accept it. – Grant Oct 09 '13 at 11:23
  • @enderland please see the update. I didn't want to use a library reference incase someone running a lesser version tries to open the program. I've run into that issue already between XP and Win7. – Grant Oct 09 '13 at 11:25

1 Answers1

1

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
David Zemens
  • 53,033
  • 11
  • 81
  • 130