将 Outlook 正文提取到 Excel VBA

Extract Outlook body to Excel VBA

在搜索了多项内容并出现错误之后 如何在 vba 脚本中按 "f5" 将电子邮件正文复制到 excel sheet /csv 其中每一行 = 下面的一个新单元格。

谢谢

抱歉,这只会给我带来麻烦。 到目前为止我尝试了什么 http://smallbusiness.chron.com/export-outlook-emails-excel-spreadsheets-41441.html

How to copy Outlook mail message into excel using VBA or Macros

http://www.vbforums.com/showthread.php?415518-RESOLVED-outlook-the-macros-in-this-project-are-disabled

http://www.ozgrid.com/forum/showthread.php?t=181512

还有一些,去年。

Outlook 对象模型无法识别正文中的线条。您可以尝试在 Outlook 中调整任何检查器 window 的大小,看看正文线条是如何变化的。

无论如何,您可以尝试使用 Word 对象模型来获取准确的行。 Outlook 使用 Word 作为电子邮件编辑器。 WordEditor property of the Inspector class returns an instance of the Document class which represents the message body. You can read more about all possible ways in the Chapter 17: Working with Item Bodies 篇文章。

How to automate Microsoft Excel from Visual Basic 文章解释了如何从任何外部应用程序实现 Excel 自动化。

这对你有用。我们基本上是根据新行将电子邮件正文拆分为一个数组。请注意,如果您在电子邮件正文中有一个空行,这将产生空白单元格。

Public Sub SplitEmail() ' Ensure reference to Word and Excel Object model is set
    Dim rpl As Outlook.MailItem
    Dim itm As Object
    Set itm = GetCurrentItem()
    If Not itm Is Nothing Then
        Set rpl = itm.Reply
        rpl.BodyFormat = olFormatHTML
        'rpl.Display
    End If
    Dim objDoc As Word.Document
    Set objDoc = rpl.GetInspector.WordEditor
    Dim txt As String
    txt = objDoc.Content.text
    Dim xlApp As Excel.Application
    Set xlApp = CreateObject("Excel.application")
    xlApp.Visible = True
    Dim wb As Excel.Workbook
    Set wb = xlApp.Workbooks.Add
    Dim i As Long
    For i = LBound(Split(txt, Chr(13)), 1) To UBound(Split(txt, Chr(13)), 1)
        wb.Worksheets(1).Range("A" & i + 1).Value = Split(txt, Chr(13))(i)
    Next i
End Sub
Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
    Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
    Case "Inspector"
    Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select
    GetCurrentItem.UnRead = False
    Set objApp = Nothing
End Function