使用Excel VBA打开Outlook .msg文件

问题描述 投票:0回答:2

我有以下代码

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

当我使用OpenSharedItem时,它会给出运行时错误438对象不支持此属性或方法。

当我使用CreateItemFromTemplate时,我收到以下错误:

无法打开文件:AUTO Andy Low Yong Cheng不在办公室(2014年9月22日返回).msg。 该文件可能不存在,您可能没有权限打开它,或者它可能在另一个程序中打开。 右键单击包含该文件的文件夹,然后单击“属性”以检查该文件夹的权限。

excel vba outlook
2个回答
0
投票

我不是百分之百关于你想要使用代码的东西,但试试这个:

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

您也可以使用.send而不是.display


0
投票

OpenSharedItem方法由Namespace对象暴露,而不是Application

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

至于第二个错误,它非常明确 - 无法找到该文件。您必须使用文件夹路径提供完全限定的文件名。您只提供文件名。

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