VBA调用Outlook发送邮件

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

' 调用Outlook发送邮件
Private Sub send_mail()
    On Error GoTo ERR_HANDLING
    Range("msg_cell").Value = "发送中..."
    Application.StatusBar = "发送中..."
    
    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
            Range("msg_cell").Value = "发送成功"
            Application.StatusBar = "发送成功"
        ' 打开outlook,然后手动点发送
        Else
            .Display
             Range("msg_cell").Value = ""
             Application.StatusBar = ""
        End If
    End With
    Exit Sub
ERR_HANDLING:
    MsgBox Err.Description
End Sub
推荐阅读
获取Excel的AddIn里边的Sheet对象
VBA发送GET、POST请求的方法
评论
1

1

4个月前

111