如何将mailitem作为msg文件嵌入到word文档中

时间:2017-03-09 19:16:51

标签: vba email outlook ms-word

我有一个带有表格的word文档,它是电子邮件的日志。第1列是电子邮件的日期,第2列是主题行,第3列我想嵌入实际的电子邮件,因此它显示为MSG文件。 这个VBA在word文档中,我已经用vba打开了邮件项目。

如何嵌入邮件项目?

Sub create_log()
Dim appOL As Outlook.Application
Dim appFolder As Outlook.Folder
Dim nsOL As Outlook.NameSpace
Dim mailItem As Outlook.mailItem
Dim items As Outlook.items
Set appOL = New Outlook.Application
Set nsOL = appOL.GetNamespace("MAPI")
Set appFolder = Session.GetDefaultFolder(olFolderInbox).Folders("Email_log")

Dim doc As Word.Document
Dim tbl As Word.Table
Dim cell As Word.cell
Dim row As Long
Dim rows_needed As Long

Set doc = Application.ActiveDocument

If doc.Tables.Count > 1 Then
    MsgBox "Too many tables, there should only be one"
    Exit Sub
End If
Set tbl = doc.Tables(1)
rows_needed = appFolder.items.Count - tbl.Rows.Count
While rows_needed > 0
    tbl.Rows.Add
    rows_needed = rows_needed - 1
Wend
Set items = appFolder.items
items.Sort "ReceivedTime", False
row = 2
For Each mailItem In items
    tbl.cell(row, 1).Range.Text = Left(mailItem.ReceivedTime, 12)
    tbl.cell(row, 2).Range.Text = mailItem.Subject
    tbl.cell(row, 3).Range.  ***how do I add mailItem*** 

    row = row + 1
Next mailItem

End Sub

这就是我想要最终结果的样子。注意单词document和table已经存在,宏只填充表格中的单元格。 enter image description here

1 个答案:

答案 0 :(得分:1)

将电子邮件保存为.msg文件,然后使用Range.InsertFile方法将路径传递给该文件。