VBScript to Create Outlook 2010 Mapi Session

Posted on 2011-05-11
Last Modified: 2012-05-11
I was running Office 2003 then 2007 and now 2010. I had a VBScript that connected to my Outlook application and then sent an email out.

Upgrading from 2007 to 2010 broke the connection. I think it has something to do with Outlook 2010 not using CDO, but this is above my head.

I'm not sure how to rewrite this to handle Outlook 2010. Would someone better versed in this be able to help?

 On Error Resume Next
 Set objSession = CreateObject("mapi.session")
 objSession.Logon ("Outlook")
 Set objMsg = objSession.Outbox.Messages.Add
 Set objRecip = objMsg.Recipients.Add
 objMsg.Subject = "Subject - " & Month(Date) & "." & Day(Date) & "." & Year(Date)
 objMsg.Text = "Messge body goes here"
 With objMsg.Attachments.Add
  .Type = 1
  .Position = -1
  .Source = XLFile
  .Name = XLFile
  .ReadFromFile XLFile
 End With
 objRecip.Name = "email address goes here"
 objRecip.Type = 1
 objRecip.Resolve False
 Set objRecip = Nothing
 Set objMsg = Nothing
 Set objSession = Nothing

Open in new window

Thank you,

Question by:Shawn Janes
    LVL 18

    Expert Comment

    does it have to connecto to outlook? why no just send it through smtp?

    Author Comment

    by:Shawn Janes
    I guess not. The end result is all that matters. I need three emails to send with an attachment.
    LVL 18

    Accepted Solution

    here's a example:
    Dim mailer : Set mailer = CreateObject("CDO.Message")
    mailer.Configuration.Fields.Item ("") = 2
    mailer.Configuration.Fields.Item ("") = "<Your SMTP server>" 
    mailer.Configuration.Fields.Item ("") = 25
    mailer.Configuration.Fields.Item ("") = ""
    mailer.From = ""
    mailer.To = ""
    mailer.Subject = "my subject"
    mailer.Textbody = "my text"
    mailer.AddAttachment "C:\temp\myattach.txt"

    Open in new window


    Author Comment

    by:Shawn Janes

    Thank you for your help the code you provided worked perfectly. I ended up having to authenticate against our remote server due to need to send one of the emails outside of our domain, but I your code led me to the final solution. Thank you.

    For others who come across this I have included a link below which includes detailed documentation on how to do this with and without authentication, local and remote services, and attachments, text, html format etc.

    Author Closing Comment

    by:Shawn Janes
    Thank you for your quick response and very good solution. I really appreciate you and your expertise.


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now