【问题标题】:Excel VB Code for Sending Email: Adding email to CC用于发送电子邮件的 Excel VBA 代码:将电子邮件添加到 CC
【发布时间】:2015-08-30 14:36:45
【问题描述】:

我正在尝试设置一个 VB 代码,它可以根据表 1 的“L”列中给出的特定电子邮件地址发送电子邮件。我面临的挑战是添加“.CC”行。我想要的“抄送”列表的“电子邮件”地址可在耻辱 excel 表“Sheet01”的 M 列中找到

有人可以建议适当的编码来将电子邮件拉到 CC 行吗?

注意:CC 列表(M 列)的长度不是静态的或变化的。

谢谢

Sub CDO_Personalized_Mail_Body()
Dim iMsg As Object
Dim iConf As Object
Dim cell As Range
  Dim Flds As Variant

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1    ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Test@gmail.com"  
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "12345@passowrd"  
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    .Update
End With

For Each cell In Sheets("Sheet1").Columns("L").Cells
        If cell.Value Like "?*@?*.?*" Then
            Set iMsg = CreateObject("CDO.Message")
            With iMsg
                Set .Configuration = iConf
                .To = cell.Value
                .From = """Test User"" <TestUser@gmail.com>" 
                .CC = Sheets("Sheet1").Columns("M").Cells  ' **here i want Insert CC line Email ID** 
                .Subject = "***Important - Email Alert***"
                .TextBody = "Hi " & vbNewLine & vbNewLine & _
                "This is Auto genrated email " & cell.Offset(0, 2).Value & vbNewLine & vbNewLine & _
                            "Thank You"
                .Send
            End With
            Set iMsg = Nothing
    End If
Next cell

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

结束子

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    范围具有 .Row 属性,该属性返回其第一行的行号。所以对于单个单元格,cell.Row 给出它的行。

    Set sh = ActiveWorkbook.Worksheets("Sheet1")
    
    For Each cell In sh.UsedRange.Columns("L").Cells
        ' CC cell: same row, column M
        Set cellCC = sh.Cells(cell.Row, "M")
        Debug.Print cell.Row, cellCC.Value
    Next cell
    

    请注意,您应该有一个 Sheet.UsedRange。在那里,或者For Each 将遍历工作表的整个 1,048,576 行。

    对于这种任务,我不喜欢使用For Each,而是在行上使用“标准”循环,如下所示,恕我直言,它的可读性更好。

    For y = 1 To sh.Columns("L").SpecialCells(xlCellTypeLastCell).Row
        Debug.Print y, sh.Cells(y, "L").Value, sh.Cells(y, "M").Value
    Next y
    

    【讨论】:

    • 感谢您的回复@Andre451.. 我会对此进行测试并让您知道
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多