excel - 如何使用 vba 将 word doc 和 excel 中的内容添加为电子邮件?
问题描述
我想要从 word doc 和 excel 文件中获取的电子邮件正文
Sub SendMailItem()
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Are you sure you want to run the macro?", vbYesNo, "Run Macro")
If Answer = vbYes Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
Dim OA As Object
Dim msg As Object
Set OA = CreateObject("Outlook.Application")
Dim i As Integer
Dim last_row As Integer
last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
For i = 2 To last_row
Set msg = OA.CreateItem(olMailItem)
Dim wd As Object, editor As Object
Dim doc As Object
Set wd = CreateObject("Word.Application")
wd.visible = True
Set doc = wd.Documents.Open(FileName:="C:\Users\usengupta\Desktop\test\Test word Document.docx", ReadOnly:=True)
doc.Content.Copy
doc.Close
Set wd = Nothing
With msg
.To = sh.Range("A" & i).Value
.CC = sh.Range("B" & i).Value
.Subject = sh.Range("C" & i).Value
.Body = olRichFormatText
.Attachments.Add sh.Range("E" & i).Value
Set editor = .GetInspector.WordEditor
editor.Content.Paste
.SentOnBehalfOfName = "xxx@abc.com"
.Display
.Send
End With
Next i
MsgBox "Mail Sent"
End If
End Sub
解决方案
推荐阅读
- cassandra - Where does Cassandra reside in the CAP theorem?
- python - 如何在 vs 代码中更改 python 代码的选项卡大小
- python - 尝试使用 Python PySFTP 模块连接到远程服务器时出现 WinError 10060 错误
- html - 如何将可滚动div的高度设置为父容器的百分比
- c++ - 删除继承中的代码重复?
- javascript - 如何使用 nodejs 在数组中追加对象?
- r - 计算 R 中每列的负值、0 值和正值的数量和百分比
- firebase - 空白 Firebase 项目连接到意外的公共数据库
- python - Django:根据域设置不同的 SESSION_COOKIE_DOMAIN
- ios - Xcode - 水平堆栈约束的垂直堆栈