2014-11-21 53 views
0

我正在嘗試使用VBA迭代某人的Outlook約會。我相信下面的代碼片段應該做什麼,我需要,但for each appm in cal.items彈出一個小消息框,指出無法使用VBA迭代其他人的約會

Laufzeitfehler '-140492795(f7a04405)' 行:

Automatisierungsfehler

在英語中,這可能是運行時錯誤....自動化錯誤

爲什麼我得到這個錯誤?

option explicit 

sub abcdef() 

    dim ol  as outlook.application 
    dim ns  as outlook.namespace 

    dim rcpt  as outlook.recipient 
    dim cal  as outlook.folder 
    dim appm  as outlook.appointmentItem 

    set ol = new outlook.application 
    set ns = ol.GetNamespace("MAPI") 
    set rcpt = ns.createRecipient("Deere John") 

    rcpt.resolve 
    if not rcpt.resolved then 
     msgBox("Could not resolve recipient") 
     return 
    end if 

    set cal = ns.getSharedDefaultFolder(rcpt, olFolderCalendar) 
    if cal is nothing then 
     msgBox ("No Calender!") 
     return 
    end if 


    for each appm in cal.items 
     ' Error occurs in previous line 
    next appm 

end sub 
+0

是因爲你有'Dim appm As Outlook.AppointmentItem',並且你正在使用'for每個apt.m文件中的? 您是否已啓用Option Explicit或強制變量聲明?如果是這樣,那麼你會看到一個錯誤,看appm <> aptm – 2014-11-21 14:14:45

+0

好點,但它不是真正的原因。我已經更改了變量名稱並添加了「顯式選項」,並且錯誤仍然存​​在。 – 2014-11-21 14:24:41

回答

0

即使你可以手動添加「迪爾約翰」到你的日曆列表,日曆權限可能是不正確的VBA。

嘗試此操作以查看Outlook是否顯示「無法顯示文件夾,Microsoft Outlook無法訪問指定的文件夾位置」。

Sub abcdef_CalDisplay() 

    Dim ol  As Outlook.Application 
    Dim ns  As Outlook.Namespace 

    Dim rcpt  As Outlook.Recipient 
    Dim cal  As Outlook.folder 
    Dim appm  As Outlook.AppointmentItem 

    Set ol = New Outlook.Application 
    Set ns = ol.GetNamespace("MAPI") 

    Set rcpt = ns.CreateRecipient("Deere John") 

    rcpt.Resolve 

    If Not rcpt.Resolved Then 
     MsgBox ("Could not resolve recipient") 
     Return 

    Else    ' <---- 
     Set cal = ns.GetSharedDefaultFolder(rcpt, olFolderCalendar) 
     cal.Display  ' <--- 

    End If 

End Sub