VBA调用Outlook发送邮件

原创文章,欢迎转载:http://miao.blog/article/vba-send-email-by-outlook

' 调用Outlook发送邮件
Private Sub SendEmail()
    On Error GoTo ErrorHandler
    
    Dim outlook_app As outlook.Application
    Dim outlook_item As outlook.MailItem
    Set outlook_app = New outlook.Application
    Set outlook_item = outlook_app.CreateItem(olMailItem)
    
    With outlook_item
        .To = Range("mail_to").Value                     ' 收件人
        If Range("mail_cc").Value <> "" Then
            .CC = Range("mail_cc").Value                 ' CC
        End If
        .Subject = Range("mail_title").Value             ' 邮件标题
        If Range("mail_file").Value <> "" Then
            .Attachments.Add Range("mail_file").Value    ' 附件
        End If
        .body = Range("mail_content").Value              ' 邮件内容
        .BodyFormat = olFormatPlain                      ' 邮件类型: 文本
        
        ' 自动发信
        If chk_auto_send.Value Then
            .Send
        ' 打开outlook,然后手动点发送
        Else
            .Display
        End If
    End With

    Exit Sub
ErrorHandler:
    MsgBox Err.Description
End Sub
推荐阅读
传统的方式配置xlam比较麻烦,这段代码可以让你双击打开xlam时自动安装或更新
评论
1

1

2019-06-13

111