[编辑] 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;
}
}
}
答案 0 :(得分:0)
设置对象的引用时,需要使用Set关键字。代码中的这一行会创建一个新的电子邮件对象(使用默认的HTML电子邮件设置): 设置thisEmail = olApp.CreateItem(MailItem)
然后这行代码不使用Set关键字: thisEmail = Item
因此,您的变量不会引用您想要的对象,而是新电子邮件。
尝试使用: 设置thisEmail = Item
或者,替换以下两行: 设置thisEmail = olApp.CreateItem(MailItem) thisEmail = Item
这条线: 设置thisEmail = Item