在 vb.net 中,当从 Outlook.Application 的实例调用方法时,弹出窗口会停止代码执行

In vb.net when calling a method from an instance of Outlook.Application, a pop up halts code execution

我们有一个 vb.net windows 应用程序,它创建 Outlook.Application 的实例以打开电子邮件 .MSG 文件,并通过调用将其保存为 .HTML Outlook.Application SaveAs() 方法。

但是,有时调用 .SaveAs() 会导致出现弹出窗口,例如,在保存经过数字签名的 .MSG 时,会出现确认弹出窗口。

此弹出窗口会导致代码执行暂停,直到用户进行交互并在弹出窗口上单击“是”或“否”。由于我们的应用程序在服务器上运行并且不需要用户交互,这会导致应用程序定期停止,直到技术人员登录到服务器并在弹出窗口中单击 'yes'。

有没有办法避免这个弹出窗口,或者让 'YES' 自动被选中? 我已经尝试调查 Outlook.Application 的文档,但当然 MSDN 上的文档非常缺乏。

下面是一些示例 vb.net 代码:

outlookapp = CreateObject("Outlook.Application")
Dim olItem As Object = outlookapp.CreateItemFromTemplate("C:\msg\message.msg")
olItem.SaveAs("C:\html\convertedmessage.html", 5)

如果 message.msg 已进行数字签名,当调用 SaveAs() 将其另存为 HTML 文件时,您会看到一个弹出窗口,提示 MSG 已进行数字签名,这会暂停代码,直到您单击是或否。

注意:Outlook 对象不支持 Word 对象的 DisplayAlerts 标志。

首先,Outlook 并非设计用于没有用户的服务或服务器端应用程序。

在您的特定情况下,您可以执行以下操作:

1 使用扩展 MAPI(仅限 C++ 或 Delphi)打开 MSG 文件(OpenIMsgOnIStg 等),然后在您的代码中显式构建 HTML 文件。

2 解析MSG文件(其格式为documented) or use one of several commercial components (such as the one from Aspose)以读取MSG文件。

3 使用Redemption (I am its author) and its RDOSession.GetMessageFromMsgFile / RDOMail.SaveAs(..., olHTML)方法打开MSG文件并进行转换。

dim Session As RDOSession = CreateObject("Redemption.RDOSession")
dim Msg As RDOMail = Session.GetMessageFromMsgFile("C:\msg\message.msg")
Msg.SaveAs("C:\html\convertedmessage.html", 5)