Kenneth Li
Kenneth Li

Reputation: 99

Opening an Outlook .msg file with Excel VBA

I have the following code

Sub Kenneth_Li()
    Dim objOL As Outlook.Application
    Dim Msg As Outlook.MailItem

    Set objOL = CreateObject("Outlook.Application")
    inPath = "C:\Users\SiliconPlus\Desktop\Si+ Contact Lists\Contact_Si+"

    thisFile = Dir(inPath & "\*.msg")
    Do While thisFile <> ""

        'Set Msg = objOL.CreateItemFromTemplate(thisFile)
        'Or
        Set Msg = objOL.OpenSharedItem(thisFile)

        Msg.display

        MsgBox Msg.Subject
        thisFile = Dir
    Loop


    Set objOL = Nothing
    Set Msg = Nothing
End Sub

When I use OpenSharedItem it gives a run-time error 438 Object doesn't support this property or method.

When I use CreateItemFromTemplate I get the following error:

Cannot open file: AUTO Andy Low Yong Cheng is out of the office (returning 22 09 2014).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.

Upvotes: 0

Views: 4538

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

OpenSharedItem method is exposed by the Namespace object, not Application.

Set objOL = CreateObject("Outlook.Application")
set objNs = objOL.GetNamespace("MAPI")
objNs.Logon
...
Set Msg = objNs .OpenSharedItem(thisFile)

As for the second error, it is pretty unambiguous - the file cannot be found. You must provider a fully qualified file name with the folder path. You are only providing the file name.

Upvotes: 0

Robert Trent
Robert Trent

Reputation: 21

I'm not 100% on what you're trying to get at with the code, but try this:

Sub LiminalMsgbx()

    Dim outappp, outmaill As Object
    Dim pthh As String
    pthh = "C:\DeskTop\MyTemplate.oft"

  Set outappp = CreateObject ("Outlook.Application")
  Set outmaill = outapp.CreateItemFromTemplate(pthh)   

                With outmaill
                    .display

                    End With
Set outappp = Nothing
Set outmaill = Nothing

End Sub

You can also use .send instead of .display.

Upvotes: 0

Related Questions