【问题标题】:Excel 2013 VBA multiple email addresses to OutlookExcel 2013 VBA 多个电子邮件地址到 Outlook
【发布时间】:2017-07-07 20:50:00
【问题描述】:

我正在使用 Excel 2013 进行编码。我有一个客户数据表,数量会随着时间的推移而增加和减少,具体取决于我活跃的资深案例: p>

A 列 - 姓氏
B 列 - 名字
C 列 - 电子邮件地址
D 列 - 等等...

我需要代码来引用 C 列并将其中的所有电子邮件放入单个 Outlook 电子邮件的密件抄送中。我创建的代码(通过我的研究)只允许将电子邮件地址硬编码到 Outlook 的 TO、CC 或 BCC 字段中——在多个条目之间使用分号。我的问题是电子邮件地址的数量会根据电子表格中的记录数量而有所不同,因此对它们进行硬编码是没有用的。下面的代码具有我需要的所有功能,但电子邮件问题除外。

Sub SendBasicEmail()
Dim olApp As Outlook.Application
Dim olEmail As Outlook.MailItem
Set olApp = New Outlook.Application
Set olEmail = olApp.CreateItem(olMailItem)
With olEmail
    .BodyFormat = olFormatHTML
    .Display
    .HTMLBody = "<h3>Testing</h3><br>" & "<br>" & .HTMLBody
    .Attachments.Add "xxx/test.pdf"
    .To = ""
    .BCC = ""
    .Subject = "Test Message"
    '.Send
End With 
End Sub

【问题讨论】:

    标签: vba excel email outlook excel-2013


    【解决方案1】:

    我只是循环遍历该列并制作包含用分号分隔的地址的字符串。

    Sub SendBasicEmail()
    Dim olApp As Outlook.Application
    Dim olEmail As Outlook.MailItem
    Set olApp = New Outlook.Application
    Set olEmail = olApp.CreateItem(olMailItem)
    
    'set your range as needed, i chose one named "recipients"
    bc_r = ""
    For each cl in range("recipients")
        bc_r = bc_r & "; " & cl.Value
    Next cl
    
    With olEmail
        .BodyFormat = olFormatHTML
        .Display
        .HTMLBody = "<h3>Testing</h3><br>" & "<br>" & .HTMLBody
        .Attachments.Add "xxx/test.pdf"
        .To = ""
        .BCC = bc_r
        .Subject = "Test Message"
        '.Send
    End With
    End Sub
    

    【讨论】:

    • 经过测试,完美满足我的需求。非常感谢您的帮助。希望我能投票给你,但这是我在 Stack Overflow 上的第一篇文章。
    【解决方案2】:

    代码将遍历工作表 1 的内容(只需更改为工作表(“whateveryoucallyoursheet”))并保留单元格行。

    Sub SendBasicEmail()
    dim ws as worksheet, y
    Dim olApp As Outlook.Application
    Dim olEmail As Outlook.MailItem
    set ws = sheets(1)
    for each y in ws.range("A1:A" & ws.range("A1").SpecialCells(xlCellTypeLastCell).row)
    
    Set olApp = New Outlook.Application
    Set olEmail = olApp.CreateItem(olMailItem)
    With olEmail
        .BodyFormat = olFormatHTML
        .Display
        .HTMLBody = "<h3>Testing</h3><br>" & "<br>" & .HTMLBody
        .Attachments.Add "xxx/test.pdf"
        .To = ws.range("A" & y.row)
        .BCC = ws.range("C" & y.row)
        .Subject = "Test Message"
        ' use display to check the email out before you send
        .display
        '.Send
    End With
    next y
    
    end sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 2014-02-14
      相关资源
      最近更新 更多