使用Excel中的VBA打开Outlook Mail .msg文件 [英] Open Outlook Mail .msg file using VBA from Excel

查看:744
本文介绍了使用Excel中的VBA打开Outlook Mail .msg文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VBA 从指定目录中打开.msg文件,但我一直遇到运行时错误.

I'm trying to open .msg files from a specified directory using VBA but I keep getting a runtime error.

我拥有的代码:

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

这是运行时错误:

运行时错误'-2147287038(80030002)':

Run-time error '-2147287038 (80030002)':

无法打开文件:AUTO Andy Low Yong Cheng不在办公室(返回2014年9月22日).msg.

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打开文件时,您没有完整的路径.试试这个:

Kenneth Li You didn't had the full path when opening the file. Try this:

Sub bla_OK()
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)
Set Msg = objOL.Session.OpenSharedItem(inPath & "\" & thisFile)
' now use msg to get at the email parts
MsgBox Msg.Subject
Set objOL = Nothing
Set Msg = Nothing
End Sub

这篇关于使用Excel中的VBA打开Outlook Mail .msg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆