Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Open *.msg-file from VBA

1,600 views
Skip to first unread message

John

unread,
Mar 17, 2002, 6:05:40 PM3/17/02
to
From Access I open .msg-files with the following code:

Dim myOlApp As Outlook.Application
Dim myitem As Object

Set myOlApp = CreateObject("Outlook.Application")
Set myitem = myOlApp.CreateItemFromTemplate(Me![file])
myitem.Display

It works in the sense, that I can open the file - but it
opens in a "mode" where I cant reply or forward the
message - it opens as if had just created the mail and can
only send it.

If I double-click the .msg file from Explorer, It opens as
if had received the mail - and I can reply and forward it
as I want to.

How do I make it open like this from VBA?

John

Truck

unread,
Mar 18, 2002, 9:16:22 AM3/18/02
to
John, I am trying to do a similar thing....With limited success... I am just
posting a mail thread which may give you and idea of what is ahead of you, I
havn't been able to get it to work the way I want, but this may give you
some Ideas....Good luck, Truck.

*********************

Ted, I took your advise and got the * following code from the MS kb, added
it in a newly created test app... it seems to work so I may have a reference
in my other app that is stopping it from working properly.

The problems I now have with CreateItemFromTemplete is that it drops the
From: (SenderName) field (expected, but this is important to what I am
trying to do..) and the attachments are not created. (..kinda expected..) I
think I will give it up as I am running short of time, and just query the
staffs *.pst files directly.

Thanks heaps for your help...
Regards,
Truck

PS. I just got this from the MS Outlook newsgroup, but I am not sure how I
would implement it?? Any Ideas... (Yeah I know I said I would give up, but I
hate doing that :-)

> About the best I can think of is to use a shellexecute on the *.msg file
and
> then reiterate the Inspectors collection to get the item. Once you have
the
> item, read the properties and close the inspector and move on to the next.

*** A little bit later....
Well I tried the above, and actually got it to work...kinda...If I have the
*.msg file already open in outlook I can use the Inspectors collection to
find all the details I want... But heres the kicker, If I use ShellExecute
(Which does open the *.msg file!) my app continues executing while Outlook
is loading the file... I tried slowing my app down (there is no _Wait_ with
ShellExecute), but when the *.msg file opens I can't get control back to my
app...aarrrggghhh. I would have posted my test app, but vb crashed and I
lost the lot...Not a happy camper!!!!

I hope all this crap makes some sense....Thnx again....

/*
Function OpenEmail()

Dim ol
Dim olns
Dim MyFolder
Dim MyItem
Dim MyForm

' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")

Set MyFolder = olns.GetDefaultFolder(olFolderInbox)

Set MyItem = ol.CreateItemFromTemplate("c:\temp\test\drafts\test.msg")

Set MyForm = MyItem.FormDescription

MyItem.Close olDiscard

End Function
*/


"Ted Harper" <tha...@nsw.bigpond.net.au> wrote in message
news:t3r99u4g45omqm1be...@4ax.com...
> "Truck" <tr...@bigpond.net.au> wrote:
>
> Hmmm, it *should* work according to a bunch of stuff I found on the
> net (even if it's not documented as working by Microsoft/MSDN). I've
> got to run off to work now, but you should do a google search (web and
> newsgroups) for "Outlook CreateItemFromTemplate msg".
>
> I just found the MS kb article Q208520
> (http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;Q208520&)
> which explicitly says that method works with *.MSG files. Have a look
> at the minimal code fragment in the article (substituting a MSG file
> for the OFT in the example) and see if it gets close. Alternatively,
> it might be worth paying the $180 to Microsoft (assuming you don't
> have Premier Support or similar and have to pay per-case) to have them
> tell you why what you're trying doesn't work, in the face of this KB
> article...
>
> ted.h.
>
> ---
> Ted Harper (Sydney, Australia)
> Business: ted.h...@btfinancialgroup.com
> Private: tha...@nsw.bigpond.net.au
> Windows/MSN Messenger: harpe...@hotmail.com
> Voice: +61-418-442-342

*********************

"John" <Jo...@nomail.com> wrote in message
news:47c501c1ce08$4201d0c0$9be62ecf@tkmsftngxa03...

Bernd Otte

unread,
Mar 18, 2002, 6:52:57 PM3/18/02
to

"John" <Jo...@nomail.com> schrieb im Newsbeitrag
news:47c501c1ce08$4201d0c0$9be62ecf@tkmsftngxa03...
[snip]

> If I double-click the .msg file from Explorer, It opens as
> if had received the mail - and I can reply and forward it
> as I want to.
>
> How do I make it open like this from VBA?
>
> John

Hi John,

I use the Shell Command (unfortunately the command line differs from OS to
OS, in Win98 you may have to use "start" I think).

The following code is running in Outlook 2000 under WinXP:

x = Shell("cmd /c c:\temp\mail.msg", vbHide)

' Wait until Inspector is open and CurrentItem is properly initialized
Do
DoEvents
Loop Until Not ActiveInspector Is Nothing
Do
DoEvents
Loop Until Not ActiveInspector.CurrentItem Is Nothing

Set myMail = Application.ActiveInspector.CurrentItem


You have to make sure that no inspector window is open, when the code is
executed. It can be done like this:

Max = Application.Inspectors.Count
If Max > 0 Then
While Application.Inspectors.Count > 0
Application.ActiveInspector.Close (olPromptForSave)
Wend
End If


HTH
Bernd


John

unread,
Mar 18, 2002, 6:58:01 PM3/18/02
to
Truck - yeah I did see you earlier post - but thought my
problem was slightly different - but I guess we could both
benefit from the same solution.

I have tried to solve this some time ago - and failed back
then... so I was just hoping for somebody to shpw up with
the magic trick.

To open a *.msg file from Access2000 I have made a
solution where I have an unbound object frame pointing to
a dummy.msg-file. Then I copy the new *.msg-file to this
dummy, and activate the unbound object frame. Then it
opens correctly - i.e I can reply and forward etc.

Only trouble is - I cant change the path to the dummy.msg
from VBA - like if I want to open the *.msg-file without
first copying it but just assigning the relevant path.

One of my colleagues tried to ask this in the Access
newsgroup a couple of days ago - but with no reply.

John

>> BusineÍ{ wÀ Ï <No $\±=ÌãD
>°ì" ss: ted.h...@btfinancialgroup.com


>> Private: tha...@nsw.bigpond.net.au
>> Windows/MSN Messenger: harpe...@hotmail.com
>> Voice: +61-418-442-342
>
>*********************
>
>"John" <Jo...@nomail.com> wrote in message
>news:47c501c1ce08$4201d0c0$9be62ecf@tkmsftngxa03...
>> From Access I open .msg-files with the following code:
>>
>> Dim myOlApp As Outlook.Application
>> Dim myitem As Object
>>
>> Set myOlApp = CreateObject("Outlook.Application")
>> Set myitem = myOlApp.CreateItemFromTemplate(Me![file])
>> myitem.Display
>>
>> It works in the sense, that I can open the file - but it
>> opens in a "mode" where I cant reply or forward the
>> message - it opens as if had just created the mail and
can
>> only send it.
>>
>> If I double-click the .msg file from Explorer, It opens
as
>> if had received the mail - and I can reply and forward
it
>> as I want to.
>>
>> How do I make it open like this from VBA?
>>
>> John
>
>

>.
>

neo [mvp outlook]

unread,
Mar 18, 2002, 10:05:39 PM3/18/02
to
John,

See the response I left to Truck's thread. I think this is the only route
you guys can go now.

"John" <jo...@nomail.com> wrote in message
news:566101c1ced8$bcd99b40$9ae62ecf@tkmsftngxa02...

>> Busineヘ{掫タ マ <No $\ア=フ絅
>ー�" ss: ted.h...@btfinancialgroup.com

Truck

unread,
Mar 19, 2002, 5:41:25 AM3/19/02
to
It works....You are a legend.....Humble thanks...Truck

* The only issue I have is the *.msg does not start hidden...I'm on Win2k
with Outlook 2k, but its a minor problem....

"Bernd Otte" <bernd...@gmx.de> wrote in message
news:a75ukp$bdo$05$1...@news.t-online.com...

john

unread,
Mar 21, 2002, 12:03:01 PM3/21/02
to
I tried it too - but with less succes...

I get the error "Object doesn't support this property or
method" in the shell-statement - and "ActiveInspector"
needs to be defined...

Am I missing a library reference or what?

John

>.
>

Truck

unread,
Mar 22, 2002, 6:02:33 AM3/22/02
to
John, here is the code fragment from my app....

Couple of things to note, On my build machine, Win2k (sp2) with VB 6 (sp5)
the "retVal = Shell(..." would work. When I transferred it to my work
machine, Win2k (No service packs) and VB 6 (No service packs), Shell doesn't
work, and I needed to put in the "retVal = ShellExecute(..." and the
ShellExecute API declare... and no, I don't have a clue why??? (Wish I
did...:-)

The other thing is that the last parameter on the SHell or SHellExecute does
not seem to have any effect on how the *.msg is displayed....it always comes
up in the Last Position and size...( another I don't know why.... )

The References I have are (Don't know which one's have any effect):

- Visual Basic for applications (MSVBVM60.DLL)
- Visual Basic runtime objects and procedures (MSVBVM60.DLL\3)
- Visual Basic objects and procedures (VB6.OLB)
- Microsoft Scripting runtime (SCRRUN.DLL)
- Microsoft Outlook 9.0 Object Library (MSOUTL9.OLB)
- OLE Automation (STDOLE2.TLB)

I hope all of this makes sense...If not send me a mail and I'll post you the
app that I built, It might not help but it can't hurt.... Regards, Truck.

/************************

Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias
"SHellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal
lpFile As String, ByVal lpParameters As String, ByVal nShowCmd As Long) As
Long

Const SW_SHOWNORMAL = 1

/*************************

Function OpenOutLookMail(FileName As String) As Boolean
Dim retVal, aFile
Dim myMail As Outlook.MailItem
Dim sQouteForCMD

sQouteForCMD = Chr(34)

on error goto EndOutLookMail

' Check if any open and close them
Check_Inspectors (olPromptForSave)

/* Uncomment the one that works....
' Shell the file and hope that the OS has the right file type...
'retVal = Shell("cmd /c " & sQouteForCMD & FileName & sQouteForCMD, vbHide)

' Shell the file and hope that the OS has the right file type...
'retVal = ShellExecute(Form1.hWnd, vbNullString, sQouteForCMD & FileName &
sQouteForCMD, "c:\temp", SW_SHOWNORMAL)


' Wait until Inspector is open and CurrentItem is properly initialized
Do
DoEvents
Loop Until Not ActiveInspector Is Nothing
Do
DoEvents
Loop Until Not ActiveInspector.CurrentItem Is Nothing

' Set the object
Set myMail = Application.ActiveInspector.CurrentItem

/*
* The *.msg file should be open and accessible here....
*/

EndOutLookMail:

' Close the one we opened, This closes the *.msg file....
Check_Inspectors (olDiscard)

' Clean-up
Set myMail = Nothing

if err.number > 0 then
msgbox ( "Error #:" & err.number & " Description: " & err.description &
vbcrlf, vbCritical + vbOkOnly, "Mail Error")
end if

End Function

/*************************

Function Check_Inspectors(Prompt As Integer)
Dim Max As Long

Max = Application.Inspectors.Count
If Max > 0 Then
While Application.Inspectors.Count > 0

Application.ActiveInspector.Close (Prompt)
Wend
End If

End Function

/*************************


john

unread,
Mar 23, 2002, 12:50:15 PM3/23/02
to
Hello Truck - now I got it to work with the help from your
code - THANKS. It turned out that the trick was to have
the statement:

retVal = Shell("cmd /c " & sQouteForCMD & FileName & _
sQouteForCMD, vbHide)

with sQouteForCMD = Chr(34)

I had tried the Shell-command before - but not with the
right parameter.

I don't "get" the whole Inspector issue though... so I
just don't do it... will that eventually cause me trouble?

John

>.
>

Truck

unread,
Mar 24, 2002, 6:20:17 AM3/24/02
to
John,

Glad I was some help... :-) The Shell command has issues dealing with long
directory / filenames unless it is enclosed in Quotes...

The inspector thingy just returns an Object so you can get some information
from whatever it is you have opened... It won't cause you any issues....

Regards,
Truck.


"john" <Jo...@nomail.com> wrote in message
news:163101c1d293$301eb9b0$3aef2ecf@TKMSFTNGXA09...

0 new messages