outlook html 纯文本格式,访问Outlook MailItem对象的文本正文 - HTML和纯文本

[编辑] 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的价值所在:

"

创建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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值