1

I am trying to save a list of email in msg format to htm format so that Xceptor tool can read the email as pdf. (Without a vba, Currently I manually open the email in Outlook and save as htm one by one.)

I found below script but I get

"Run Time error 287: Application-defined or object-defined error".

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
Community
  • 1
  • 1
jun
  • 21
  • 2
  • Does this answer your question? ["Run-time error '287': Application-defined or object-defined error" while using CurrentItem.Saveas or CurrentItem.HTMLBody in Outlook 2016](https://stackoverflow.com/questions/57204574/run-time-error-287-application-defined-or-object-defined-error-while-using) – niton Jun 25 '21 at 17:12

1 Answers1

1

Without a vba, i need to open the email in outlook and save as htm one by one.

Without VBA? So I guess you want to use VBScript? Say, directly run it from desktop by clicking on the .vbs file? If yes, then you need to declare and create/get the outlook object before you can work with it. Otherwise how will your code know what is Session? Also olHTML and olDiscard are Outlook constants. VbScript will not know what they are.

Is this what you are trying? Paste this in Notepad and save it as 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
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Thank you so much for your guidance. My original script I put it in Excel visual basic. But I have followed your instruction to use the suggested script in notepad and save in desktop as .vbs file. When I run in desktop, i have below error:- Script: C:\Users\43918084\Desktop\emailmacro.vbs Line:22 Char: 1 Error: Operation aborted Code:80004004 Source : (null) – jun Jun 23 '21 at 05:36
  • The above code is tried and tested. It works just fine for me. Line 22 is `olMsg.Close olDiscard` Did you change anything in the code? BTW did it create the html inspite of the error because the line previous to that creates the html. – Siddharth Rout Jun 23 '21 at 05:42
  • Thank you very much again for your help. I copied exactly the same code and even put the email in C:\Temp\ and change the email anme as test.msg. Unfortunately, the email was not converted to htm. I wonder if it relate to any security setting in Outlook by the company? – jun Jun 24 '21 at 06:06
  • Could be the security settings. What happens if you remove the line `olMsg.Close olDiscard`? – Siddharth Rout Jun 24 '21 at 07:06
  • Thanks a lot for your patience to my problem. I just note that my line 22 is olMsg.SaveAs strPath & strHTML, olHTML. Just wondering if there is any work around if it is due to security setting? – jun Jun 25 '21 at 08:06
  • What is the value of `strPath` & `strHTML` – Siddharth Rout Jun 25 '21 at 08:54