We help IT Professionals succeed at work.

VBA code to choose mail account

Kapusta asked
I want to be able to have my VBA choose an Outlook 2000 mail account for sending emails.  The following code will send an email programmatically from within Outlook 2000:

            Set objMail = Application.CreateItem(olMailItem)
            With objMail
                ' By default person will be added
                ' to the To area
                .Recipients.Add (EmailAddress)
                .Subject = SubjectText
                .Body = TheBody
                ' Send it
                Set objMail = Nothing
            End With

But this code does not allow me to choose the mail account or specify the "Reply to" or "Sender" values.  I need to have control over these parameters.
Watch Question

What mode is Outlook configured in?


>> What mode is Outlook configured in?

Internet POP/SMTP only.   We do not use Exchange.  Is that what you wanted to know?

Yep.  Gives me something to work with since there isn't anything in the object model to specify account.  Going to check to see if there is another way to do this.

I'm not sure about how to add it to your VBA code, but it is user selectable from Options... button or View > Options... in a new message.



>> but it is user selectable from Options..

Yes, I understand.  But that does not help me when I am trying to use VBA code and select the corresponding account from within the code.
Setting the sending account programattically is not possible under any version of Outlook using the Outlook Object Model.  However, there might be a way to do this using Collaboration Data Objects.

I will do a quick synopsis to see if you are interested. (PS - Testing was done with Outlook 2000 Internet Mail Only mode.)

1) Messages will have to be generated and saved to the Drafts folder. (Actually you can use any predefined place.)

2) Release all mailitem/Inspector objects in regards to items dropped into drafts folder.

3) Establish a CDO session and go to the Drafts folder.

4) Read account settings from registry if you want to display an account picker to user ~ otherwise just hard code the account information into your code.

5) Go through each message in Drafts folder and set two custom MAPI properties on the message. (This is where you are setting the sending account information.)

6) Send the message (or process all in this folder and return to Outlook and run through the messages again to send).

Explore More ContentExplore courses, solutions, and other research materials related to this topic.