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

后端 未结 2 1929
没有蜡笔的小新
没有蜡笔的小新 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: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
    

提交回复
热议问题