通过电子邮件发送范围而不是行

时间:2016-10-12 09:02:19

标签: excel vba excel-vba runtime-error excel-2010

我正在尝试运行Excel工作簿的第2页,向客户发送电子邮件范围。

范围为A1:B30,C1:D30,E1:F30等,其账号为A1& B1中的电子邮件和以下信息。

每当我尝试运行电子邮件时,它都会出现:

  

运行时错误1004

然后继续错误

  

对象已被移动或删除

是否有其他方式通过电子邮件发送范围或修改此代码?

Sub EmailRanges()
Dim cr As Range
Set cr = [b1]
ActiveWorkbook.EnvelopeVisible = True
Do While cr <> ""
    cr.Offset(, -1).Resize(30, 2).Select
    With ActiveSheet.MailEnvelope
        .Introduction = " Good Morning"
        .Item.To = cr
        .Item.Subject = "Just testing, sorry for filling you inbox ^_^ "
        .item.Send                                 ' to send
        .Item.Display                               ' to test
    End With
    MsgBox cr & " receives " & Selection.Address
    Set cr = cr.Offset(, 2)
Loop
Application.ScreenUpdating = True
MsgBox "The Customers Have Been Notified"
End Sub

1 个答案:

答案 0 :(得分:1)

您需要更明确地了解您的参考资料(工作簿,工作表......)。

转到@Ralph:

  

只有首先激活工作表时才能选择范围。否则,您将收到错误。

这在我的电脑上顺利运行:

Sub Email_Ranges()
    Dim rG As Range
    Dim RangeToSend As Range
    Dim CustomerMail As String

    Set rG = ActiveWorkbook.ActiveSheet.[b1]

    ActiveWorkbook.EnvelopeVisible = True

    Do While rG.Value <> vbNullString
        CustomerMail = rG.Value
        Set RangeToSend = rG.Offset(, -1).Resize(30, 2)

        'With RangeToSend.Parent.MailEnvelope

        ''Uncomment below if you get an error
        rG.Parent.Activate
        RangeToSend.Select
        With Selection.Parent.MailEnvelope

            .Introduction = "Good Morning"
            With .Item
                .To = CustomerMail
                .Subject = "Just testing, sorry for filling your inbox ^_^ "
                .display    'to test
                .Send      'to send
            End With
        End With
        Debug.Print CustomerMail & " receives " & RangeToSend.Address
        Set rG = rG.Offset(, 2)
    Loop

    ActiveWorkbook.EnvelopeVisible = False
End Sub