Reply to .msg file with Excel VBA

625 views Asked by At

I have a .msg file saved locally and I'm trying to reply to it using Excel VBA. I have this code, which just takes the .msg file as a template, but does not reply to it.

I googled a lot, but I am not sure how to do this.

Any help would be appriciated!

Sub ReplyMSG()
Dim objOL As Object
Dim Msg As Object

Set objOL = CreateObject("Outlook.Application")
inPath = "C:\temp"

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

    Set Msg = objOL.CreateItemFromTemplate(inPath & "\" & thisFile)
    Msg.Display
    thisFile = Dir

Loop

Set objOL = Nothing
Set Msg = Nothing
End Sub
1

There are 1 answers

0
niton On
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Sub replyMsg()

Dim objOL As Object
Dim objNamespace As Object

Dim msg As Object
Dim replyMsg As Object

Dim inPath As String
Dim thisFile As String

Set objOL = CreateObject("Outlook.Application")
Set objNamespace = objOL.GetNamespace("MAPI")

inPath = "C:\temp"

thisFile = Dir(inPath & "\*.msg")
Debug.Print thisFile

Do While thisFile <> ""
    
    Set msg = objNamespace.OpenSharedItem(inPath & "\" & thisFile)
    
    Set replyMsg = msg.Reply
    
    msg.Close 1     ' olDiscard https://learn.microsoft.com/en-us/office/vba/api/Outlook.OlInspectorClose
    Set msg = Nothing

    replyMsg.display
    thisFile = Dir
    
Loop

Set objOL = Nothing
Set objNamespace = Nothing

End Sub