首页 > 解决方案 > 带有 MailEnvelope 的 VBA 停止工作

问题描述

您能否就以下问题给我建议。

我有一个带有 VBA 的 excel 工作簿,运行时应该将仪表板通过电子邮件发送给列表中的电子邮件收件人。

现在宏停止工作。Or rather the macro starts to work, but does not generate the email: When run, it asks if an email should be sent, but when "yes" is selected nothing happens?! 我能够运行旧版本的工作簿及其宏。由于宏正在使用活动工作表,因此它执行宏并使用当前仪表板发送电子邮件。但是,当我将代码粘贴到当前工作簿时,它并没有完全执行。不会生成电子邮件。因此,当前工作簿似乎已禁用某些内容。一位同事在仪表板中做了一些更改——添加了行和列,然后在 VBA 中调整了范围。否则我不知道有任何其他变化。

先感谢您。

请看下面的代码:

'Send Bulk Email From Excel Using VBA Code
Sub SendDailyReport()
    'Below Loop can be changed to While Loop or increase the limit (10) if your list has more than 10 mail ids
    Dim SendTo As String
    Dim ToMSg As String
 If MsgBox("Are you sure you want to send this report?", vbYesNo) = vbNo Then Exit Sub
    For I = 2 To 1000
        SendTo = ThisWorkbook.Sheets(1).Cells(I, 1)
        If SendTo <> ñî Then
            ToMSg = ThisWorkbook.Sheets(1).Cells(I, 2)
            Send_Range SendTo, ToMSg
        End If
    Next I
End Sub
Sub Send_Range(SendTo As String, ToMSg As String)


   ' Select the range of cells on the active worksheet.
   ActiveSheet.Range("F4:S46").Select

   ' Show the envelope on the ActiveWorkbook.
   ActiveWorkbook.EnvelopeVisible = True

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

   ' Set the optional introduction field thats adds
   ' some header text to the email body. It also sets
   ' the To and Subject lines. Finally the message
   ' is sent.
   With ActiveSheet.MailEnvelope
      .Introduction = ""
      .Item.To = SendTo
      .Item.Subject = ToMSg
      .Item.Send
   End With

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

End Sub

标签: vbaexcel

解决方案


的主题ToMSg取自这里:

ToMSg = ThisWorkbook.Sheets(1).Cells(I, 2)

因此,它取自工作簿的第一页,而不是ActiveSheet.

删除ThisWorkbook.Sheets(1)它应该可以工作。


推荐阅读