在Outlook Mail中添加多个CC

时间:2016-08-15 06:37:54

标签: excel vba excel-vba email outlook

我有这行代码我试图在Outlook Mail中添加多个CC。但它只返回;。我在MSDN找到了这个样本。

Dim ccMail as String
Dim ccRow as Long
Dim objMail as Object
ccRow = Cells(Rows.count, 16).End(xlUp).Row

With objMail
        .Subject = Sheet1.TextBox1.Value
        For k = 4 To ccRow
            ccMail = ccMail & ";" & Cells(k, 1).Value
        Next k
        .cc = ccMail
end with

所有CC收件人都在P列中找到。 有什么帮助吗?谢谢。

2 个答案:

答案 0 :(得分:1)

您使用了With objMail但没有为Cells(k, 1).Value指定工作表。这可能会导致错误。

此外,我认为你想引用ws.Cells(k, 16),因为你想要的是P列。

答案 1 :(得分:1)

以下是如何使用Do-Until Loops循环获取单元格值的示例。

Option Explicit
Sub Example()
    Dim olApp As Object
    Dim olMail As Object
    Dim olRecip As Object
    Dim iRow As Long
    Dim Recip As String
    Dim Sht As Worksheet

    iRow = 2

    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.CreateItem(0)
    Set Sht = ActiveWorkbook.Sheets("Sheet1")

    With Sht
        With olMail
             Do Until IsEmpty(Cells(iRow, 16))
                 Recip = Cells(iRow, 16).Value
                 Set olRecip = .Recipients.Add(Recip)
                     olRecip.Type olCC
                     olRecip.Resolve
                 iRow = iRow + 1
             Loop

            .Subject = "Subject"
            .Body = "Hi " & .Body
            .Display
        End With
    End With

    Set olApp = Nothing

End Sub

Do-Until loop and IsEmpty