0

我正在尝试添加位于一系列单元格上的多个收件人的电子邮件。

我可以选择工作表上的电子邮件范围。

但是,我一直收到这个不匹配错误,我不知道如何解决它。

我一直在寻找解决方案并做了同样的步骤。

请原谅我,我是 VBA 新手。非常感谢您的帮助。我的代码如下,

    Private Sub CommandButton1_Click()

        Dim olapp As Object
        Dim olmail As Object
        Dim recip As String


    lastr = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 
    'this is for the range of data to be copied on the body but have yet to do it

    lastr2 = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 7).End(xlUp).Row


        recip = ThisWorkbook.Sheets("Sheet1").Range("G3:G" & lastr2).Value 
        'mismatch after this step

        Set olapp = CreateObject("Outlook.Application")

        Set olmail = olapp.CreateItem(0)

        With MItem
         .to = recip
         .Subject = "hello"
         .Body = "whats up"
         .display
         End With

知道为什么会这样吗?

4

1 回答 1

1

您正在尝试将一个数组(多个单元格的范围是一个数组)分配给一个字符串变量。正如 Jaycal 的评论所建议的那样,无需测试,我知道您可以通过循环解决此问题:For Each

Dim cl as Range
For each cl in ThisWorkbook.Sheets("Sheet1").Range("G3:G" & lastr2).Cells
    recip = recip & ";" & cl.Value    
Next

但是您可以通过使用字符串Join函数来简化。该Join函数有效地对字符串数组执行此循环,因此它为您节省了不必要的循环。我修改为使用范围变量以提高可读性:

Dim sendRange as Range
Set sendRange = ThisWorkbook.Sheets("Sheet1").Range("G3:G" & lastr2)
recip = Join(Application.Transpose(sendRange.Value), ";")

无论您使用哪种方法,您都可以使用相同的With块。

    With MItem
     .to = recip
     .Subject = "hello"
     .Body = "whats up"
     .display
     End With
于 2013-11-13T14:30:12.133 回答