2017-02-16 122 views
1

我想從Excel工作表中更新共享日曆。我對這個共享日曆的擁有者的代碼工作正常,但它對我來說失敗了。日曆已分享給我,我擁有完整的所有者權限。無法更新共享的非默認日曆文件夾

我可以手動編輯日曆但不存在任何問題,但想法是任何人都可以從此Excel表中運行宏來更新共享日曆。下面是相關的代碼,只是到故障點:

Sub UpdateSched() 

Dim olApp As Outlook.Application 
Dim olNameSpace As Outlook.Namespace 
Dim olFolder As Outlook.MAPIFolder 
Dim olFldrOwner As Outlook.Recipient 

On Error Resume Next 
' check if Outlook is running 
Set olApp = GetObject("Outlook.Application") 
If Err <> 0 Then 
    'if not running, start it 
    Set olApp = CreateObject("Outlook.Application") 
End If 
On Error GoTo 0 

Set olNameSpace = olApp.GetNamespace("MAPI") 
Set olFldrOwner = olNameSpace.CreateRecipient("ownrAlias") 
    olFldrOwner.Resolve 

Set olFolder = Nothing 

If olFldrOwner.Resolved Then 
    Set olFolder = olNameSpace.GetSharedDefaultFolder(olFldrOwner, olFolderCalendar) 

' If olFolder Is Nothing Then 
'  Debug.Print "Nothing" 
' Else 
'  Debug.Print olFolder.Name '<-Error here if the if-block is run 
' End If 

    '****************************** 
    Set olFolder = olFolder.Folders("Transport Sched") '<-Object Not Found Error 
    '****************************** 
End If 

'Code below updates appointments on the shared calendar 

完整的錯誤是「嘗試的操作失敗。無法找到對象'

爲了測試,我添加了註釋掉的塊。這讓我覺得這個錯誤實際上可能在上一行。當這個塊被取消註釋時,在Else之後的代碼錯誤出現在同一個錯誤上(同樣的錯誤)。所以olFolder對象並不是什麼都沒有,但是它找不到。

再次,當日歷的所有者運行它時,這一切都可以正常工作。它在我的「共享日曆」下,我收到錯誤。如果我使用相同的代碼來更新我創建的日曆,那麼它也可以正常工作,這在「我的日曆」下。

這對於找到共享日曆的正確文件夾有問題,對吧?該文件夾的路徑不應該改變,所以我可以對它進行硬編碼以適合每個人,這是可能的嗎?

+0

試一下這個'設置olFolder = olNameSpace.GetSharedDefaultFolder(olFldrOwner,olFolderCalendar).Folders(「運輸附表」)'讓我知道 – 0m3r

+0

當我嘗試如你所說,現在無法上線,以同樣「沒有找到對象'錯誤。這實際上是這條線的原始寫法,在閱讀另一個答案之後,我已經將它分成兩行來幫助調試。 – jvarnerus

回答

1

我想出了一個解決方案來解決我的問題,但方式與我試用問題中列出的代碼完全不同。如果任何人需要它,這裏是解決方案:

Sub ListCalendars() 
Dim olApp As Outlook.Application 
Dim olPane As Outlook.NavigationPane 
Dim olModule As Outlook.CalendarModule 
Dim olGroup As Outlook.NavigationGroup 
Dim olNavFolder As Outlook.NavigationFolder 
Dim olFolder As Folder 
Dim i As Integer, j As Integer 

On Error Resume Next 
' check if Outlook is running 
Set olApp = GetObject("Outlook.Application") 
If Err <> 0 Then 
    'if not running, start it 
    Set olApp = CreateObject("Outlook.Application") 
End If 
On Error GoTo 0 

Set olPane = olApp.ActiveExplorer.NavigationPane 
Set olModule = olPane.Modules.GetNavigationModule(olModuleCalendar) 
Set olGroup = olModule.NavigationGroups.GetDefaultNavigationGroup(olMyFoldersGroup) 

'Dummy Do loop allows exit from within nested For-Next loops 
Do 
For i = 1 To olModule.NavigationGroups.Count  'Cycle through all Nav Groups 
    Set olGroup = olModule.NavigationGroups.Item(i) 
    Debug.Print olGroup.Name 

    For j = 1 To olGroup.NavigationFolders.Count 'Cycle through all calendars in group 
     Set olNavFolder = olGroup.NavigationFolders.Item(j) 
     Debug.Print " - " & olNavFolder.DisplayName 

     'Un-comment If-block below if searching for a particular calendar: 
      'CalendarName is the name of the calendar,as listed in your navigation pane 
'   If olNavFolder.DisplayName = "CalendarName" Then 
'   Debug.Print "Found it!" 
'   Set olFolder = olNavFolder.Folder 'To get folder object from NavigationFolder 
'   Exit Do 
'  End If 

    Next 
Next 
Exit Do 'To prevent endless loop 
Loop While True 

'If-block below displays results if looking for matching calendar name 
'If olFolder Is Nothing Then 
' Debug.Print vbNewLine & "No match found" 
'Else 
' Debug.Print vbNewLine & "Matching calendar found: " & olFolder.Name 
'End If 

End Sub 

此代碼是從this page here修改。基本上,即使日曆已被共享,訪問其他人的日曆文件夾對象也會直接給我提出問題。儘管使用了各種導航對象,但我能夠瀏覽導航窗格中列出的所有日曆,包括所有共享日曆。

提供此例程僅列出主文件夾和一級子文件夾。如果兩個if塊沒有註釋,那麼例程將搜索具有給定名稱的日曆(只需替換CalendarName),並顯示是否找到匹配。

虛擬Do循環是突破嵌套循環的一種方式。還有其他多種方法可以完成此列表in this SO question

另一個棘手的問題是NavigationFolder對象與Folder對象不一樣。這個看似無關緊要的是:如果你想更改日曆文件夾,因爲這兩個對象類型有不同的屬性和方法

Set olFolder = olNavFolder.Folder 

其實是非常重要的。