How to open MSG file from VBA in OL 2003

  • Thread starter Thread starter Shaun Merrill
  • Start date Start date
S

Shaun Merrill

I'm programming in VB.NET 2003 using the Microsoft.Office.Interop.Outlook
namespace.
I noted that a PST file is opened directly via the command-line switch like
this:

OUTLOOK.EXE /f <pstFileName>

I wish to perform that same action using VB automation. That is, I wish to
programatically open a MSG file. I have tried the following:
Application.CreateItemFromTemplate(filename) but no matter what I choose, it
doesn't seem to open the file. Perhaps only certain types of files are
opened this way?

Thanks
~ Shaun
 
CreateItemFromTemplate only works on OFT files (Outlook form template file).
It does not work with MSG files.

Other than double-clicking on an MSG file there isn't a lot you can do with
it in the Outlook object model. You can use Redemption
(www.dimastr.com/redemption) to import a MSG file using code into an Outlook
item. For example SafeContactItem.Import <filepath>, 3 will import a contact
saved as a MSG file into an Outlook contact item.

There's a code example on the Redemption site that shows how to import a MSG
into an Outlook item.
 
Actually I learned from Dmitry that CreateItemFromTemplate does work on .msg
files, but it doesn't "open" the .msg file. Instead, it creates a new item
using the .msg file as a template, so it's definitely not the same as
double-clicking it. Shell.Run would be another way to open an .msg file,
which I'd expect to appear in the Inspectors collection.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
I guess you also could use the Win32 ShellExecute with the "open" argument
in that case.
 
Thank you both for your kind and thorough responses. My objective is to
deal with the loose MSG file as a MailItem object. I am developing in
VS.NET Enterprise Architect 2003 for OL 2003. Your threads lead me in three
directions:

1. KEN: If I used either Shell.Run, Win32 ShellExecute, or DDE, how do I
programmatically make that into a MailItem so I can feed it into my MailItem
code (exactly as if it were found inside a MapiFolder)? (I see that I might
need to programmatically "Copy To Folder" in order to guarantee that.)
Please point me in the right direction if you would.

2. KEN: Checking into "Extended MAPI" at Microsoft lead me to the MFCMapi
gadget and sourcecode. Inside there, I found that there is a
LoadFromMSG(Filename, IMessage) method, but I run into three problems,
because I am a VB programmer, not a C++ programmer. Problem A is that the
MFCMapi project compiles to an EXE instead of a DLL and therefore cannot be
"referenced" from VB.NET. Problem B is that I could choose a new project to
port the LoadFromMSG() source code into, but I do not know which type of
project to start with. Problem C is that I really don't know how to create
an IMessage so I can call the method.

3. SUE: If I use the CreateItemFromTemplate method on a loose MSG file, I am
finding this intermittent and sometimes gives the error:

"Error writing e-mail Attachment filename (<filename.msg>): Can't open file:
<filename.MSG>. The file may not exist, you may not have permission to open
it, or it may be open in another program. Right-click the folder that
contains the file, and then click Properties to check your permissions for
the folder.; STACK TRACE: at
Microsoft.Office.Interop.Outlook._Application.CreateItemFromTemplate(String
TemplatePath, Object InFolder) at ..."

My question to SUE is, If the file is templated instead of opened, what is
the list of properties that may differ from the original MSG file, if you
can tell me?

I apologize if that is too many directions to go in one thread. (1a, 1b, 1c,
2, 3).
Thanks,
~ Shaun Merrill, Senior Database Architect in Bremerton, WA
 
1) If you execute the .msg file, then its window will appear in the
Inspectors collection and you should be able to copy it to an Outlook
folder. I've used this technique successfully with .vcf files, but haven't
tried it with .msg files.

2) Use Redemption, which is an ExMAPI COM wrapper that can import an .msg
file.

3) That depends on the item. If it's a message, it will appear as an new,
unsent draft rather than a saved message.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Thanks, that was it.

Sue Mosher said:
1) If you execute the .msg file, then its window will appear in the
Inspectors collection and you should be able to copy it to an Outlook
folder. I've used this technique successfully with .vcf files, but haven't
tried it with .msg files.

2) Use Redemption, which is an ExMAPI COM wrapper that can import an .msg
file.

3) That depends on the item. If it's a message, it will appear as an new,
unsent draft rather than a saved message.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
1. I use Redemption for that and its Import method for various types of
items.

2. I use Redemption to be able to code in VB instead of C++.
 
Back
Top