0
votes

I have a VBScript which opens Outlook 2016 and sends a message.

The problem I have is that I have several Outlook profiles.

I would like to set the actual profile I wish to open from which to send the message.

My existing script is:

Dim objOutlook, objEmail

Dim strEmailReceiver, strEmailCc, strEmailBcc, strEmailSubject, strEmailBody, strEmailAttachments

Set objOutlook = CreateObject("Outlook.Application") Set objEmail = objOutlook.CreateItem(0)

strEmailSubject=InPutBox("Input your message") With objEmail

.To = "[email protected]"

' .Cc = strEmailCc

' .Bcc = strEmailBcc

.Subject = strEmailSubject

' .Body = strEmailBody

' If (strEmailAttachments <> "") Then

' .Attachments.Add strEmailAttachments

' End If

.Send

End With

'Clear the memory

Set objOutlook = Nothing

Set objEmail = Nothing

I wish to use the profile called CEO

If outlook is closed I get a messagebox asking which profile to use, once selected the script works. This is the step I wish to avoid.

1

1 Answers

0
votes

Immediately after creating an instance of the Outlook.Application object, add code like the following

Set objOutlook = CreateObject("Outlook.Application") 
set objNS = objOutlook.GetNamespace("MAPI")
objNS.Logon("The Profile name")
Set objEmail = objOutlook.CreateItem(0)

keep in mind that if Outlook is already running, Namespace.Logon will do nothing and you will end up with the running instance of Outlook (since it is a singleton) using whatever profile it was using at the moment