I'm trying to open .msg files from a specified directory using VBA but I keep getting a runtime error.
The code i have:
Sub bla()
Dim objOL As Object
Dim Msg As Object
Set objOL = CreateObject("Outlook.Application")
inPath = "C:\Users\SiliconPlus\Desktop\Si+ Contact Lists\Contact_Si+"
thisFile = Dir(inPath & "\*.msg")
Set Msg = objOL.CreateItemFromTemplate(thisFile)
' now use msg to get at the email parts
MsgBox Msg.Subject
Set objOL = Nothing
Set Msg = Nothing
End Sub
Here is the runtime error:
Run-time error '-2147287038 (80030002)':
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.
Kenneth Li You didn't had the full path when opening the file. Try this:
You should check follow code and can modify your code
Another way is to run the file programmatically (in VBA use the
Shell
command). It will be opened in Outlook where you can get an active inspector window with the item opened.If you get an error, try the Late Biding (
Dim Msg As Object
) right under theMsgBox
(need to be uncommented) :Or you can find a nice VB solution there : http://www.mrexcel.com/forum/excel-questions/551148-open-msg-file-using-visual-basic-applications.html#post2721847
And here for more details on
Shell
method : http://p2p.wrox.com/access-vba/27776-how-open-msg-file-vbulletin.html#post138411Try this
Edited : How to add the reference
Click Tools > Reference. Check the needed reference