We help IT Professionals succeed at work.

VBA code to choose mail account

Kapusta
Kapusta asked
on
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
                objMail.Send
                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.
Comment
Watch Question

Commented:
What mode is Outlook configured in?

Author

Commented:
>> What mode is Outlook configured in?

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

Commented:
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.

Commented:
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.

Colleen

Author

Commented:
>> 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.
Commented:
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.