【发布时间】:2014-04-26 22:03:48
【问题描述】:
[编辑] 这个问题在 C# 中似乎不存在。请参阅底部的重新编写的代码。
这让我困惑了两天,终于让我在这里发表了我的第一篇文章。
我在 Excel 2007 的 Visual Basic 编辑器中编码。 我使用的是 Excel 2007 中的 Outlook 2007 对象库,而不是 Outlook。不确定这是否重要。
我正在编写一个程序,该程序将定期在邮件文件夹上运行,并在电子邮件到达时解析出电子邮件中的重要信息。有问题的电子邮件看起来像纯文本,但 VBA Locals 窗口将其识别为 olFormatHTML!为了测试这一点,我单击其中一封电子邮件的“回复”,然后尝试将 Excel 范围粘贴到电子邮件正文中,Outlook 给了我一个弹出窗口(兼容性检查器),让我可以选择“切换到 HTML” .看起来像纯文本。此外,打开消息,单击“其他操作”-> 编码产生 Unicode (UTF-8)。那么,当我在 Locals 窗口中展开这个 MailItem 对象时,为什么 Excel 会认为它是 HTML 电子邮件呢?
此 MailItem 的 .Body 为空,此 MailItem 的 .HTMLBody 不包含电子邮件的实际内容,通过 Outlook 查看时为非空。下面是 HTMLBody 的值:
"<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=ProgId content=Word.Document><met"
用于创建 Outlook 应用程序对象、导航到所需文件夹并将 MailItem 对象传递给解析器的代码(如果您熟悉这部分,请跳过):
' Navigate to desired folder and pass information to text analyzer.
Sub AccessInbox(timeStamp As Date)
Dim olApp As Outlook.Application
Dim objNamespace As Outlook.Namespace
Dim objFolder As Outlook.MAPIFolder
Dim sharedFolder As Outlook.MAPIFolder
Set olApp = New Outlook.Application
Set objNamespace = olApp.GetNamespace("MAPI")
' Explicitly went through hierarchy since I'll be using with a shared Mailbox eventually.
Set objMailbox = objNamespace.Folders("Mailbox - My Name")
Set objFolder = objMailbox.Folders("Inbox")
Set sharedFolder = objFolder.Folders("Folder With Stuff")
'mostly irrelevant, see ParseEmailText code below this
Dim emailTimeStamp As Date
For Each Item In sharedFolder.Items
' Inbox can contain other kinds of objects than MailItem.
If TypeOf Item Is MailItem Then
Dim thisEmail As Object
Set thisEmail = olApp.CreateItem(MailItem)
thisEmail = Item
' Check to see if email has already been analyzed.
emailTimeStamp = thisEmail.ReceivedTime
If emailTimeStamp > timeStamp Then
' Send to email text analyzxer.
ParseEmailText thisEmail
Else
Exit For
End If
End If
Next
End Sub
解析电子邮件正文的代码:
Sub ParseEmailText(email As Outlook.MailItem)
emBody = email.Body
' This is the part where I wish I could just access the email's body, but it is empty.
End Sub
[编辑] 我在 C# 中重新编写了这个基本代码,MailItem.Body 不再是空白的。事实上,它完全按预期工作。知道为什么 VBA 如此糟糕吗?
class Parser
{
//Outlook variables
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder atFolder = null;
public Parser()
{
}
public void ParseInbox()
{
//open outlook
//Access Outlook (only need to do this once)
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI"); //Returns a NameSpace object of the specified type. The only supported name space type is "MAPI".
ns.Logon(null, null, false, false); //Namespace.Logon method: Logs the user on to MAPI, obtaining a MAPI session.
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
atFolder = inboxFolder.Folders["Folder With Stuff"];
for (int i = atFolder.Items.Count; i > 0; i--)
{
item = (Microsoft.Office.Interop.Outlook.MailItem)atFolder.Items[i];
string emailText = item.Body;
}
}
}
【问题讨论】:
标签: vba excel outlook outlook-2007