VBA Excel根据呼叫值发送电子邮件

时间:2015-06-12 20:57:27

标签: excel vba excel-vba

我对使用VBA有点新意,我能够为我正在尝试的大部分内容找到合适的代码。

我在Excel中创建了一个排序目录,每个月我都要向多个客户端发送单独的附件。

除了能够使用我保存的Outlook签名外,我还能正常工作。

我发现了一些使用签名的编码,但我不知道如何将其与我所拥有的相结合。

我的代码到目前为止:

    Sub SendEmail() 
    Dim OutlookApp As Object 
    Dim MItem As Object 
    Dim cell As Range 
    Dim email_ As String 
    Dim cc_ As String 
    Dim subject_ As String 
    Dim body_ As String 
    Dim attach_ As String 

     'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application") 

     'Loop through the rows
    For Each cell In Columns("a").Cells.SpecialCells(xlCellTypeConstants) 

        email_ = cell.Value 
        subject_ = cell.Offset(0, 1).Value 
        body_ = cell.Offset(0, 2).Value 
        cc_ = cell.Offset(0, 3).Value 
        attach_ = cell.Offset(0, 4).Value 



         'Create Mail Item and send it
        Set MItem = OutlookApp.CreateItem(0) 
        With MItem 
            .To = email_ 
            .CC = cc_ 
            .Subject = subject_ 
            .Body = body_ 
            .Attachments.Add attach_ 
             '.Display
        End With 
        MItem.Send 
    Next 
End Sub 

2 个答案:

答案 0 :(得分:0)

在Outlook中添加默认签名

    'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0)
    With MItem              '<-----Added
        .Display            '<-----Added
    End With                '<-----Added
    signature = MItem.body  '<-----Added

    With MItem
        .To = email_
        .CC = cc_
        .Subject = subject_
        .body = body_ & vbNewLine & signature ' <-----Added (& vbNewLine & signature)
        .Attachments.Add attach_
         '.Display
    End With

答案 1 :(得分:0)

如果将Outlook设置为在新邮件上生成签名,则

.Body = body_ & .Body