【问题标题】:Save msg file as htm将 msg 文件另存为 htm
【发布时间】:2021-06-22 04:07:52
【问题描述】:

我正在尝试将 msg 格式的电子邮件列表保存为 htm 格式,以便 Xceptor 工具可以将电子邮件读取为 pdf。 (没有vba,目前我在Outlook中手动打开邮件并一一保存为htm。)

我在下面找到了脚本,但我得到了

“运行时错误 287:应用程序定义的或对象定义的错误”。

Sub SaveMSG_as_HTML()

    Dim olMsg As MailItem
    Dim strPath As String
    Dim strMsg As String
    Dim strHTML As String
        
    strPath = "\\Hbap.adroot.hsbc\hk\Finance\224017\AMH_A2R_2\WRK\AAC\PL\To GFC\Movement Table\MvtXceptor\Configuration\Table18.1\"
    strMsg = "RE  CRR Inquiry as atJan-00-00 - --.msg"
    strHTML = Left(strMsg, InStrRev(strMsg, Chr(46))) & "html"
    Set olMsg = Session.OpenSharedItem(strPath & strMsg)
    olMsg.SaveAs Path:=strPath & strHTML, Type:=olHTML
    olMsg.Close olDiscard
lbl_Exit:
    Set olMsg = Nothing
    Exit Sub

End Sub

【问题讨论】:

标签: excel vba outlook


【解决方案1】:

如果没有vba,我需要在outlook中打开邮件并一一保存为htm。

没有 VBA?所以我猜你想使用VBScript?比如说,通过单击.vbs 文件直接从桌面运行它?如果是,那么您需要先声明并创建/获取 Outlook 对象,然后才能使用它。否则你的代码怎么知道Session是什么? olHTMLolDiscard 也是 Outlook 常量。 VbScript 不会知道它们是什么。

这是你正在尝试的吗?将此粘贴到记事本中并保存为Sample.Vbs

Private Const olHTML = 5
Private Const olDiscard = 1

Dim OutApp
Dim olMsg
Dim nsOutlook

Dim strPath
Dim strMsg
Dim strHTML
  
'~~> I used these values for testing. Change as applicable
strPath = "C:\Temp\"
strMsg = "test.msg"
strHTML = "test.html"

Set OutApp = CreateObject("Outlook.Application")
Set nsOutlook = OutApp.GetNamespace("MAPI")
Set olMsg = nsOutlook.OpenSharedItem(strPath & strMsg)

olMsg.SaveAs strPath & strHTML, olHTML
olMsg.Close olDiscard

Set olMsg = Nothing
Set nsOutlook = Nothing
Set OutApp = Nothing

【讨论】:

  • 非常感谢您的指导。我的原始脚本我把它放在 Excel Visual Basic 中。但我已按照您的指示在记事本中使用建议的脚本并将桌面另存为 .vbs 文件。当我在桌面上运行时,出现以下错误:- 脚本:C:\Users\43918084\Desktop\emailmacro.vbs Line:22 Char: 1 Error: Operation aborted Code:80004004 Source : (null)
  • 上面的代码经过测试。它对我来说很好用。第 22 行是 olMsg.Close olDiscard 您是否更改了代码中的任何内容?顺便说一句,尽管出现错误,它还是创建了 html,因为之前的行创建了 html。
  • 再次非常感谢您的帮助。我复制了完全相同的代码,甚至将电子邮件放入 C:\Temp\ 并将电子邮件名称更改为 test.msg。不幸的是,电子邮件未转换为 htm。我想知道这是否与公司在 Outlook 中的任何安全设置有关?
  • 可能是安全设置。如果删除 olMsg.Close olDiscard 行会发生什么?
  • 非常感谢您对我的问题的耐心等待。我只是注意到我的第 22 行是 olMsg.SaveAs strPath & strHTML, olHTML。只是想知道是否有任何解决方法,如果它是由于安全设置?
猜你喜欢
  • 2011-01-26
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多