【发布时间】:2018-06-06 01:07:30
【问题描述】:
您能否就以下问题给我建议。
我有一个带有 VBA 的 Excel 工作簿,运行时应该将仪表板通过电子邮件发送给列表中的电子邮件收件人。
现在宏停止工作。或者更确切地说,宏开始工作,但不生成电子邮件: 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
【问题讨论】:
-
将“.Item.Send”更改为“.Item.Display”。这会显示电子邮件正文吗?
-
这个花哨的命名变量-
ñî的值是多少?声明了吗? -
@underbot,我改为“Item.Display”,但它没有改变任何东西。但是根据下一条评论取得了一些进展。谢谢。
-
@Vityata 谢谢你的提示。我虽然这意味着“” - 空。我确实将它替换为“结束”只是为了尝试将 MailEnvelope 实际加载到 Excel 中。然后我收到此错误:“运行时错误 1004。范围类的选择方法失败。当我选择调试时,问题似乎是范围/VBA 行:ActiveSheet.Range("F4:S46").Select
-
@Vityata 在 EmailEnvelope 的主题中实际上是来自不同工作表上的单元格的文本!我实际上尝试了几次并运行带有错误的VBA。在一次尝试中,我收到了来自此代码的大量电子邮件,其中包含正确的仪表板,但主题来自另一张工作表中的单元格。请问有什么想法吗?