Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open .msg file in an Outlook object 1

Status
Not open for further replies.

fredmartinmaine

IS-IT--Management
Mar 23, 2015
79
US
I have a need - using vbscript - to create an Outlook object and open a message currently stored in an .msg file.

Spent a lot of time on the web trying to find out how to do this and although you'd think you could just create an Outloook Message object and open it in there, it seems to be a lot more complex.

Did a lot of poking around to discover the OpenSharedItem method:

objOutlook = CreateObject("Outlook.Application")
set oNamespace = objOutlook.GetNamespace("MAPI")
oNamespace.OpenSharedItem("H:\Projects\Assigned-Risk.msg")

This runs without error but I can't get beyond this. oNamespace seems to hold the email message, or pointers to it at least, but I haven't the slightest idea of how to stuff it into an email message, or read it in any other way.

What I'm really trying to do is save that .msg file as an .mht (html) file, which if I manually open the .msg with Outlook, I can easily do with "save as". I'm just trying to automate that process. User picks an .msg file, and the script saves it as an .mht.

Thanks in advance for any help,
Fred
 
I found this, which looks far more promising. It gets that msg into an Outlook Mail object alright.

MsgFile = "H:\Projects\Assigned-Risk.msg"

Set objShell = CreateObject("scripting.filesystemobject")
Set objOutlook = CreateObject("Outlook.Application")
Set objMailItem = objOutlook.CreateItemFromTemplate(MsgFile)

objMailItem.saveas "H:\Projects\Assigned-Risk.html"

The saveas creates the html file and that supporting folder with html images and other stuff, I'll deal with that.

Fred
 
Try Changing

[tt]oNamespace.OpenSharedItem("H:\Projects\Assigned-Risk.msg")[/tt]

to something like

[tt]Set Msg=oNamespace.OpenSharedItem("H:\Projects\Assigned-Risk.msg")
msg.SaveAs "H:\Projects\Assigned-Risk.mht", olHTML
[/tt]
where

OlSaveAsType values:
olDoc = 4
olHTML = 5
olICal = 8
olMSG = 3
olMSGUnicode = 9
olRTF = 1
olTemplate = 2
olTXT = 0
olVCal = 7
olVCard = 6
 
Changed it per your suggestion, I ran this:

set objOutlook = CreateObject("Outlook.Application")
set oNamespace = objOutlook.GetNamespace("MAPI")
Set Msg=oNamespace.OpenSharedItem("H:\Projects\Assigned-Risk.msg")
msg.SaveAs "H:\Projects\Assigned-Risk.mht", olHTML

It runs without error but the .mht file is empty. However, changing the filename to Assigned-Risk.html works fine, also creating the folder though.

I also discovered, there's a difference between these:

set oNamespace = objOutlook.GetNamespace("MAPI")
oNamespace = objOutlook.GetNamespace("MAPI")

The latter doesn't work. I assumed that 'set' was like the 'let' assignment in the old Basic, where it's optional. Not the case here apparently.

 
>It runs without error but the .mht file is empty

Oops - use olMHTML (which has a value of 10) instead

>I assumed that 'set' was like the 'let' assignment in the old Basic,

Set is very different, and is used for objects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top