×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Open .msg file in an Outlook object

Open .msg file in an Outlook object

Open .msg file in an Outlook object

(OP)
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

RE: Open .msg file in an Outlook object

(OP)
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

RE: Open .msg file in an Outlook object

Try Changing

oNamespace.OpenSharedItem("H:\Projects\Assigned-Risk.msg")

to something like

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

where

OlSaveAsType values:
olDoc = 4
olHTML = 5
olICal = 8
olMSG = 3
olMSGUnicode = 9
olRTF = 1
olTemplate = 2
olTXT = 0
olVCal = 7
olVCard = 6

RE: Open .msg file in an Outlook object

(OP)
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.

RE: Open .msg file in an Outlook object

>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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close