Sending email through MS Access VBA / Outlook, choosing sending profile

后端 未结 2 1931
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 23:09

I am looking at this snippet of code from another question here (MS Access VBA): https://stackoverflow.com/a/17975507/1085885

Right now this code only works when I r

相关标签:
2条回答
  • 2021-01-17 00:08

    You need to log to the specified profile. After creating an instance of the Outlook application

    Set oApp = CreateObject("Outlook.application")
    

    add something like the following:

    set oNS = oApp.GetNamespace.Logon
    oNS.Logon("MyProfileName")
    

    Note if Outlook is already running, Logon will do nothing. You will need to use Extended MAPI (C++ or Delphi or a MAPI wrapper like Redemption (RDOSession.Logon) to log to a specified profile.

    Why not work with a single profile and create multiple accpunts? You can then set the MailItem.SendUsaingAccount property to specify a particular account.

    0 讨论(0)
  • 2021-01-17 00:09

    Try it this way.

    Private Sub Command1_Click()
    
    Dim bStarted As Boolean
    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem
    
    On Error Resume Next
    
    
    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        'Outlook wasn't running, start it from code
        Set oOutlookApp = CreateObject("Outlook.Application")
        bStarted = True
    End If
    
    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    
    With oItem
        'Set the recipient for the new email
       .To = "receiver@gmail.com"
    
        .Send
    End With
    
    If bStarted Then
    '    'If we started Outlook from code, then close it
        oOutlookApp.Quit
    End If
    
    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing
    
    
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题