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/off...amespace.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
2020-09-28_12-38-29.jpg

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.