簡體   English   中英

使用Excel VBA打開Outlook .msg文件

[英]Opening an Outlook .msg file with Excel VBA

我有以下代碼

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不在辦公室(返回22 09 2014).msg。
該文件可能不存在,您可能沒有打開它的權限,或者它可能已在另一個程序中打開。
用鼠標右鍵單擊包含該文件的文件夾,然后單擊屬性以檢查您對該文件夾的權限。

OpenSharedItem方法由Namespace對象而不是Application公開。

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

至於第二個錯誤,它非常明確-找不到文件。 您必須提供具有文件夾路徑的標准文件名。 您僅提供文件名。

對於您要使用的代碼,我不是100%的人,但是請嘗試以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM