locked
Having Access work with Outlook RRS feed

  • Question

  • I have two questions:

    1. I'm learning how to send Outlook emails through Access. Here is my code so far,

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem

    'Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")
    'Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

    With objOutlookMsg
    .To =myemail@gmail.com
    .Subject = "This is an Automation test with Microsoft Outlook"
    .Body = "This is the body of the message." & vbCrLf & vbCrLf
    .Send
    End With

    Set objOutlook = Nothing

    This works fine. My problem is I have multiple email accounts within Outlook. How do I place in the code which email account should be used for sending the message?

    2. I would like to automate my process within Access to move emails from 1 subfolder into another subfolder. Right now, the code below requires me to select the origin and destination folder.

    Dim appOutlook As Outlook.Application
    Dim nms As Outlook.NameSpace
    Dim fld As Outlook.MAPIFolder
    Dim fld2 As Outlook.MAPIFolder

    Set appOutlook = GetObject(, "Outlook.Application")
    Set nms = appOutlook.GetNamespace("MAPI")
    Set fld = nms.PickFolder
    Set fld2 = nms.PickFolder

    While fld.Items.Count > 0
    fld.Items(1).Move fld2
    Wend

    How do I code in the name and locations for both the origin and destination folders.

     

    Thank you in advance!

    Thursday, September 29, 2011 1:05 AM

Answers

  • Thanks!! I wrote code to solve my problem. My code was about 10 lines. I'll try your's instead.
    • Marked as answer by Greg Crowe Sunday, October 2, 2011 4:35 AM
    Sunday, October 2, 2011 12:30 AM

All replies

  • 1. Set the MailItem.SendUsingAccount property - http://msdn.microsoft.com/en-us/library/bb207787(v=office.12).aspx

    2. Automate in what sense? What are the source and target folders supposed to be? When are the mesages supposed to be moved? If you want to move them after they are sent, set the MailItem.SaveSentMessageFolder property before calling Send.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/
    OutlookSpy - Outlook, CDO
    and MAPI Developer Tool
    Thursday, September 29, 2011 2:16 AM
  • For #2, within Access, I have a table which is linked to the following folder: \\Dispatch-SCAT\Inbox\New. After I've done some manipulating with the data, and copied to another table, I'm trying to move the emails into the another folder: \\Dispatch-SCAT\Inbox\Pending. With the code I have in existance, I have to manually select the origination and destination folders. Since these are always the same, I'd like to automate this process. Thanks!
    Thursday, September 29, 2011 3:53 AM
  • Try

    Set fld   = nms.Folders.Item("Dispatch-SCAT").Folders.Item("Inbox").Folders.Item("New")
    Set fld2 = nms.Folders.Item("Dispatch-SCAT").Folders.Item("Inbox").Folders.Item("Pending")


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/
    OutlookSpy - Outlook, CDO
    and MAPI Developer Tool
    Sunday, October 2, 2011 12:25 AM
  • Thanks!! I wrote code to solve my problem. My code was about 10 lines. I'll try your's instead.
    • Marked as answer by Greg Crowe Sunday, October 2, 2011 4:35 AM
    Sunday, October 2, 2011 12:30 AM