Consulting

Results 1 to 3 of 3

Thread: Initiate Instance of OUTLOOK without having to choose profile.

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location

    Initiate Instance of OUTLOOK without having to choose profile.

    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.
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Graham,

    Thanks. That works.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •