[英]Extract Outlook body to Excel VBA
after searching multiple things, and getting errors How do I upon pressing "f5" in a vba script copy the body of an email into an excel sheet /csv where every line = a new cell below.在搜索多项内容并出现错误后,如何在 vba 脚本中按“f5”将电子邮件正文复制到 Excel 表 /csv 中,其中每一行 = 下面的一个新单元格。
Thanks谢谢
Sorry, this is causing me nothing but trouble.对不起,这只会给我带来麻烦。 What I have tried so far http://smallbusiness.chron.com/export-outlook-emails-excel-spreadsheets-41441.html
到目前为止我尝试过的http://smallbusiness.chron.com/export-outlook-emails-excel-spreadsheets-41441.html
How to copy Outlook mail message into excel using VBA or Macros 如何使用 VBA 或宏将 Outlook 邮件消息复制到 Excel 中
http://www.vbforums.com/showthread.php?415518-RESOLVED-outlook-the-macros-in-this-project-are-disabled 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 http://www.ozgrid.com/forum/showthread.php?t=181512
and a few more, last year.还有一些,去年。
This will work for you.这对你有用。 we are basically splitting the email body into an array based on a new line.
我们基本上是将电子邮件正文拆分为基于新行的数组。 Notice that this will yield blank cells if you had a blank line in the email body.
请注意,如果电子邮件正文中有空行,这将产生空白单元格。
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
The Outlook object model doesn't recognize lines in the body. Outlook 对象模型无法识别正文中的线条。 You can try to resize any inspector window in Outlook and see how the body lines are changed.
您可以尝试在 Outlook 中调整任何检查器窗口的大小,并查看正文线条是如何更改的。
Anyway, you may try to use the Word object model to get the exact lines.无论如何,您可以尝试使用 Word 对象模型来获取准确的线条。 Outlook uses Word as an email editor.
Outlook 使用 Word 作为电子邮件编辑器。 The WordEditor property of the Inspector class returns an instance of the Document class which represents the message body.
Inspector 类的WordEditor属性返回表示消息正文的 Document 类的实例。 You can read more about all possible ways in the Chapter 17: Working with Item Bodies article.
您可以在第 17 章:使用项目实体一文中阅读有关所有可能方式的更多信息。
The How to automate Microsoft Excel from Visual Basic article explains how to automate Excel from any external application.如何从 Visual Basic 自动化 Microsoft Excel 一文解释了如何从任何外部应用程序自动化 Excel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.