使用Excel中的VBA打开Outlook Mail .msg文件

问题描述 投票:8回答:5

我正在尝试使用VBA从指定目录打开.msg文件,但我不断收到运行时错误。

我有的代码:

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)':

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

该文件可能不存在,您可能没有权限打开它,或者它可能在另一个程序中打开。右键单击包含该文件的文件夹,然后单击“属性”以检查该文件夹的权限。

excel vba outlook outlook-vba
5个回答
3
投票

Kenneth Li你打开文件时没有完整的路径。试试这个:

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

2
投票

如果您收到错误,请尝试在qazxsw poi下面的Late Binding(qazxsw poi)(需要取消注释):

Dim Msg As Object

或者你可以在那里找到一个很好的VB解决方案:MsgBox

在这里有关Sub Kenneth_Li() Dim objOL As Outlook.Application Dim Msg As Outlook.MailItem Msgbox "If you get an error, try the Late Biding right under this (need to be uncommented)" 'Dim objOL As Object 'Dim Msg As Object Set objOL = CreateObject("Outlook.Application") inPath = "C:\Users\SiliconPlus\Desktop\Si+ Contact Lists\Contact_Si+" thisFile = LCase(Dir(inPath & "\*.msg")) Do While thisFile <> "" 'Set Msg = objOL.CreateItemFromTemplate(thisFile) 'Or 'Set Msg = objOL.OpenSharedItem(thisFile) 'Set Msg = GetNameSpace("MAPI").OpenSharedItem(thisFile) 'Eventually with Shell command (here for notepad) 'Shell "notepad " & thisFile Set Msg = objOL.Session.OpenSharedItem(thisFile) Msg.display MsgBox Msg.Subject thisFile = Dir Loop Set objOL = Nothing Set Msg = Nothing End Sub 方法的更多细节:http://www.mrexcel.com/forum/excel-questions/551148-open-msg-file-using-visual-basic-applications.html#post2721847


1
投票

另一种方法是以编程方式运行文件(在VBA中使用Shell命令)。它将在Outlook中打开,您可以在其中打开项目的活动检查器窗口。


0
投票

0
投票

试试这个

Shell

编辑:如何添加引用 单击工具>参考。检查所需的参考

© www.soinside.com 2019 - 2024. All rights reserved.