PDA

View Full Version : [SOLVED:] Initiate Instance of OUTLOOK without having to choose profile.



gmaxey
09-28-2020, 09:48 AM
I am trying to help a person automatically send an Email (from Word). He has two profiles defined for Outlook. There is no problem if his OUTLOOK application is already running, but if we have to create and start an instance of OUTLOOK then he is getting a prompt to choose a profile and then gets an error on the line of code below marked "****"

I did some research online and based on this:
https://docs.microsoft.com/en-us/office/vba/api/outlook.namespace.logon
I thought that a new instance of OUTLOOK would be created, avoid the prompt to choose profile and send the message just as if OUTLOOK was already running.
That does not happen. He is still getting the prompt, and still getting the error on the line identified.



Sub InitializeMAPI()
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim mailFolder As Outlook.Folder
Dim EmailItem As Outlook.MailItem
Dim Doc As Document
Dim strMsgBody As String

'Start Outlook.
Set olApp = CreateObject("Outlook.Application")
'Get a session object.
Set olNs = olApp.GetNamespace("MAPI") '****"
'Create an instance of the Inbox folder. If Outlook is not already running, this has the side effect of initializing MAPI.
Set mailFolder = olNs.GetDefaultFolder(olFolderInbox)
Set EmailItem = olApp.CreateItem(olMailItem)
Set Doc = ActiveDocument
With EmailItem
.Subject = "Test"
strMsgBody = strMsgBody & "Dear Greg" & vbCr & vbCr
strMsgBody = strMsgBody & "Hopefully this works." & vbCr & vbCr
strMsgBody = strMsgBody & "Kind Regards," & vbCr
strMsgBody = strMsgBody & "Sam"
.Body = strMsgBody
.To = "test@test.com"
.Importance = 1
.Attachments.Add Doc.FullName
.Send
End With
Application.ScreenUpdating = True
Set Doc = Nothing
olApp.Quit
Set olApp = Nothing
Set EmailItem = Nothing
lbl_Exit:
Exit Sub
End Sub

27225

Can any explain or show how to create a new instance if OUTLOOK (start OUTLOOK if not already running) and avoid the prompt to choose a profile by 1) automatically using the default profile or 2) automatically choosing a named profile?

Thanks.

gmayor
09-29-2020, 12:19 AM
See http://www.rondebruin.nl/win/s1/outlook/openclose.htm
and
https://stackoverflow.com/questions/17976340/sending-email-through-ms-access-vba-outlook-choosing-sending-profile#17976482

gmaxey
09-29-2020, 05:38 AM
Graham,

Thanks. That works.