从excel范围vba添加邮件正文
Posted
技术标签:
【中文标题】从excel范围vba添加邮件正文【英文标题】:add mail body from excel range vba 【发布时间】:2018-05-09 23:24:27 【问题描述】:我有一个数据库,只有姓名和电子邮件地址,我需要使用特定的固定文本创建单独的电子邮件,该文本位于我的 Excel 的特定范围内,并将其保存在我硬盘的特定文件夹中开车。
但是,我遇到了正文问题。
代码如下:
Sub test()
Dim Sendrng As Range
Dim outlookApp As Outlook.Application
Dim outlookMail As Outlook.MailItem
Dim i As Integer
For i = 2 To ActiveSheet.Cells(5, 2).Value + 1
Set Sendrng = ActiveSheet.Range("B12:K29")
Set outlookApp = New Outlook.Application
Set outlookMail = outlookApp.CreateItem(olMailItem)
With outlookMail
.To = ActiveSheet.Cells(7, 2).Value
.Subject = ActiveSheet.Cells(8, 2).Value
'.htmlBody = Sendrng
.Save
End With
i = i + 1
ActiveSheet.Cells(4, 2) = i
Set outlookMail = Nothing
Set outlookApp = Nothing
Next i
'I want to start every time in position 2
ActiveSheet.Cells(4, 2) = 2
End Sub
你能告诉我哪里不见了吗?我收到以下错误:运行时错误 13:类型不匹配
谢谢, 恩里克
【问题讨论】:
【参考方案1】:您需要使用Range
类的Text
属性而不是Value
:
Sub test()
Dim Sendrng As Range
Dim outlookApp As Outlook.Application
Dim outlookMail As Outlook.MailItem
Dim i As Integer
For i = 2 To ActiveSheet.Cells(5, 2).Value + 1
Set Sendrng = ActiveSheet.Range("B12:K29")
Set outlookApp = New Outlook.Application
Set outlookMail = outlookApp.CreateItem(olMailItem)
With outlookMail
.To = ActiveSheet.Cells(7, 2).Text
.Subject = ActiveSheet.Cells(8, 2).Text
.Save
End With
i = i + 1
ActiveSheet.Cells(4, 2) = i
Set outlookMail = Nothing
Set outlookApp = Nothing
Next i
'I want to start every time in position 2
ActiveSheet.Cells(4, 2) = 2
End Sub
【讨论】:
以上是关于从excel范围vba添加邮件正文的主要内容,如果未能解决你的问题,请参考以下文章
VBA 展望。尝试从电子邮件正文中提取特定数据并导出到 Excel
VBA 在 Outlook 中粘贴带有图表的特定 excel 范围
使用Excel VBA发送带有图表对象的电子邮件 - Office 2013