vba - 带有 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
解决方案
的主题ToMSg
取自这里:
ToMSg = ThisWorkbook.Sheets(1).Cells(I, 2)
因此,它取自工作簿的第一页,而不是ActiveSheet
.
删除ThisWorkbook.Sheets(1)
它应该可以工作。
推荐阅读
- java - 如何在 symmetricDS 社区版运行时动态添加引擎
- julia - 符号在 Julia 中有何用处(除了比使用字符串更高效)?
- firebase - 在 Python 中有效地解析数据存储实体
- git - Netlify 环境变量中的 JSON
- c - 在 Windows 中编译 C:TDM 与 MinGW
- java - 在嵌入式上使用远程 janusgraph 连接时的优缺点是什么?
- javascript - 使用原始打印打印简体中文字符
- jenkins - Jenkins -OpenShift 中的 Kubernetes 插件。只有 jnlp 容器工作
- git - 如何在 Android Studio 的 Github 中放弃本地提交?
- javascript - 两种方式数据绑定在 Kendo UI 上不起作用