【问题标题】:How can I embed a mailitem into a word document as a msg file如何将邮件项作为 msg 文件嵌入到 word 文档中
【发布时间】:2017-07-30 22:19:15
【问题描述】:

我有一个带有表格的word文档,它是电子邮件的日志。第 1 列是电子邮件的日期,第 2 列是主题行,在第 3 列我想嵌入实际的电子邮件,以便它显示为 MSG 文件。 这个 VBA 在 word 文档中,我已经在 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

这就是我希望最终结果的样子。注意word文档和表格已经存在,宏只是填充表格中的单元格。

【问题讨论】:

  • 你能在你的 word doc 中添加一个图片示例吗?

标签: vba email outlook ms-word


【解决方案1】:

将电子邮件保存到 .msg 文件,然后在 Range.InsertFile 方法中传递该文件的路径。

【讨论】:

  • 我尝试了 Range.InsertFile 但最终以二进制文本归档 word 文档。 'tbl.cell(row, 3).Range.InlineShapes.AddOLEObject olMSG, fileName, False, True, "C:\Program Files (x86)\Microsoft Office\Office14\1033\OUTLLIBR.DLL", 35, subject' 似乎可以工作,但现在 ICON 引用是硬编码的。
  • 您可以通过其他方式获取该 .dll 的路径,因此它不会被硬编码。您为 InsertFile 使用了哪些参数?
  • 保存为 MSG 文件,然后插入确实有效。但是我使用了 AddOLEObject 而不是 insertFile 方法。我用来插入图标的参数是“C:\Program Files (x86)\Microsoft Office\Office14\1033\OUTLLIBR.DLL”,35是outlook exe文件的路径,35是图标的索引,我只是遍历值,直到找到我想要的图标。
猜你喜欢
  • 2015-01-29
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 2019-08-29
  • 1970-01-01
相关资源
最近更新 更多