Link to home
Start Free TrialLog in
Avatar of Senniger1
Senniger1

asked on

VBA SendObject from Access and Outlook 2010, but Display Message before Sending

We just upgraded from Office 2003 to Office 2010.  So I now have Access 2010 and Outlook 2010.

I used the sendobject method to send an email from Access to Outlook.   Here is my code.

   DoCmd.SendObject , , acFormatTXT, From, CCTo, BCCTo, stSubject, stText, False

I'd like to be able to view the email before sending it in case I want to type extra text.  Research shows "Display" is what I'm wanting to do, but I cannot figure out how to apply it to my code.

Any help would be appreciated.

Thanks!
Avatar of omgang
omgang
Flag of United States of America image

expression.SendObject(ObjectType, ObjectName, OutputFormat, To, Cc, Bcc, Subject, MessageText, EditMessage, TemplateFile)

DoCmd.SendObject(, , , CCTo, BCCTo, stSubject, StText, True)

OM Gang
Take a look at VBA Help for the SendObject method.  It provides the details for all the parameters.  In your case I left out the parameter OutputFormat because you're not sending any type of object - that's why I removed the acFormatTXT.
Hope this helps.
OM Gang
Avatar of Senniger1
Senniger1

ASKER

Thank you for your help.  I had done all the above previously before posting and couldn't get anything to work.

Here's my simplified code.  What I discovered is if Outlook 2010 is closed, the code works fine.  If I have Outlook open, then the code doesn't work.  

Private Sub cmdTestEmail_Click()
On Error GoTo Err_cmdTestEmail_Click
    'Write the e-mail content for sending to assignee
        DoCmd.SendObject , , , "JDoe@senniger.com", , , "Subject", "Body of Email", True
Exit_cmdTestEmail_Click:
      Exit Sub
Err_cmdTestEmail_Click:
      Resume Exit_cmdTestEmail_Click
End Sub

Any idea why this is happening?

Thanks!
It works as expected for me in Access 2007/Outlook 2007.  I can test this evening in Access 2010/Outlook 2010.
OM Gang
Any luck with testing in Access 2010/Outlook 2010?

I ended up finding another solution since I wasn't sending any type of object (code below).  This seems to work well for this situation, but it would be nice to know how to use the SendObject in future projects.
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
       StrBody = "<html><head></head><body>"
       StrBody = StrBody & "***** Body of Email" *****" _
       StrBody = StrBody & "</body></html>"
    Set OutMail = OutApp.CreateItem(0)
    OutMail.To = From
    OutMail.CC = CCTo
    OutMail.BCC = BCCTo
    OutMail.Subject = stSubject
    OutMail.HTMLBody = StrBody
    OutMail.Display 'Send | Display
    Set OutMail = Nothing

Thanks so much for your help!
The advantage to using SendObject in your situation is the code is more portable, e.g. it will/should work with the default mail client on the machine, Outlook Express, Windows Live Mail, etc.  The Outlook automation code depends upon Outlook being installed on the machine and won't work if it is not.  Still, a good solution if it works for you.  I forgot to check out SendObject in Office 2010 at home.  I'm sending myself a message as a reminder so I'll do it this evening.

OM Gang
ASKER CERTIFIED SOLUTION
Avatar of omgang
omgang
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the delay.  Let me test your proceedure on Windows XP which is what we're using and I'll get back to you.
When I tested your code I got a Run-time error '2293'.

Upon further investigation, I believe my issue is the fact that when I right click on a file on my desktop and select Send To, Mail Recipient, it doesn't work.  I suspect this may be my issue.  I'll work to repair this issue.

Thank you so much for you help and I did use the information you provided.
Make sure Outlook (or Windows Live Mail, Outlook Express, etc.) is configured as the default mail program on the computer/for the user profile.  In Windows 7 there is a Default Programs applet in Control Panel.  I'll check on Windows XP here in a bit.

Thanks,
OM Gang
In Windows XP the applet is located in the Add or Remove Programs dialogue.  It's titled set Program Access and Defaults.  You may also have a link to it in your start menu.  Make sure you have a default mail program specified.

OM Gang