选择Outlook电子邮件粘贴到Word文档-VBA宏



我想:

  1. 选择来自某个发件人的所有电子邮件

  2. 将电子邮件正文复制到新的Word文档

  3. 将word文档保存到特定目录

  4. 清除剪贴板

我想知道我还需要做什么,特别是在FindSetAside()SaveToDicrectory()函数中。

Sub FindSetAside()  'find all set-aside emails
End Sub
Sub PasteToWord()
    Dim Word As Word.Application
    Dim Doc As Word.Document
    Dim activeMessage As Outlook.MailItem 'the email to copy
    Dim activeBody As String
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then
    'get the active email
    Set activeMessage = ActiveExplorer.Selection.Item(1)
    'setup Word
    Set Word = CreateObject("Word.Application")
    WordApp.Visible = True
    setDoc = Word.Documents.Add
    'Copy selection to document
    activeMessage.GetInspector().WordEditor.Range.FormattedText.Copy
    Doc.Range.Paste
    Call ClearClipboard
End If
End Sub
Sub SaveToDirectory() 'Save the Word Document to the correct directory
End Sub
Public Sub ClearClipboard()
    Dim Data As New DataObject
    Data.SetText Tex:=Empty
    Data.PutInClipboard
End Sub

我正在使用Outlook 2010。我也在考虑添加一些代码,将Word文档作为特定电子邮件的附件发送,但也许这与这个问题无关。

如果您需要对某些电子邮件执行搜索,则使用Explorer.Selection将没有帮助(除非您使用Explorer.search,它在UI中执行搜索)。看看这个可以帮助确定你需要使用的搜索方法:

https://msdn.microsoft.com/EN-US/library/ff869846.aspx

然后只需要遍历返回的集合并访问其中的MailItem对象。

若要保存Word文档,请使用document.SaveAs2。

最新更新