Outlook VSTO - 获取组日历中的所有项目
Outlook VSTO - Get all items in a group calendar
我正在寻找一种在 Outlook VSTO 加载项中访问组日历的方法。我只能在 outlook 中搜索默认日历。我在下面分享了一张来自 outlook 的图片,其中显示了多个群组日历。如何从 VSTO 代码访问这些组日历?
您可以在 Outlook 中使用 NavigtionModule 对象来访问组日历(或共享日历)。 NavigationModule 对象提供对 Microsoft Outlook 导航窗格中显示的各种导航模块的访问。例如:
Sub Test()
Const olFolderCalendar = 9
Const olModuleCalendar = 1
Const olAppointmentItem = 1
Dim answer As Integer
Dim objNS
Dim objExpCal
Dim objNavMod
Dim objNavGroup
Dim objNavFolder
Dim objFolder
Dim colExpl
Set oApp = CreateObject("Outlook.Application")
Set objNS = oApp.Session
Set colExpl = oApp.Explorers
Set objExpCal = objNS.GetDefaultFolder(olFolderCalendar).GetExplorer
Set objNavMod = objExpCal.NavigationPane.Modules.GetNavigationModule(olModuleCalendar)
For Each objNavGroup In objNavMod.NavigationGroups
For Each objNavFolder In objNavGroup.NavigationFolders
If Not objNavFolder = "YOUR CALENDAR NAME" Then 'exactly as in Outlook
GoTo NxtGroup
End If
On Error Resume Next
Set objFolder = objNavFolder.Folder
NxtGroup:
Next
Next
MsgBox objFolder.Name
Set objNS = Nothing
Set objNavMod = Nothing
Set objNavGroup = Nothing
Set objNavFolder = Nothing
Set objFolder = Nothing
Set colExpl = Nothing
End Sub
添加 C# 版本的答案,因为最初的问题是针对 C# 提出的
Outlook.NameSpace mapiNamespace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
Outlook.CalendarModule calendarModule = (Outlook.CalendarModule)mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).GetExplorer().NavigationPane.Modules.GetNavigationModule(Outlook.OlNavigationModuleType.olModuleCalendar);
Outlook.NavigationGroups navGroups = calendarModule.NavigationGroups;
foreach (Outlook.NavigationGroup navGroup in navGroups)
{
Outlook.NavigationFolders navFolders = navGroup.NavigationFolders;
foreach (Outlook.NavigationFolder navFolder in navGroup.NavigationFolders)
{
try
{
if (navFolder.Folder != null && navFolder.Folder.Name.Equals("<Add your group calendar name here>"))
{
//do stuff here
return;
}
}
catch (Exception ex)
{
//handle exception as needed and iterate to next
continue;
}
}
}
我正在寻找一种在 Outlook VSTO 加载项中访问组日历的方法。我只能在 outlook 中搜索默认日历。我在下面分享了一张来自 outlook 的图片,其中显示了多个群组日历。如何从 VSTO 代码访问这些组日历?
您可以在 Outlook 中使用 NavigtionModule 对象来访问组日历(或共享日历)。 NavigationModule 对象提供对 Microsoft Outlook 导航窗格中显示的各种导航模块的访问。例如:
Sub Test()
Const olFolderCalendar = 9
Const olModuleCalendar = 1
Const olAppointmentItem = 1
Dim answer As Integer
Dim objNS
Dim objExpCal
Dim objNavMod
Dim objNavGroup
Dim objNavFolder
Dim objFolder
Dim colExpl
Set oApp = CreateObject("Outlook.Application")
Set objNS = oApp.Session
Set colExpl = oApp.Explorers
Set objExpCal = objNS.GetDefaultFolder(olFolderCalendar).GetExplorer
Set objNavMod = objExpCal.NavigationPane.Modules.GetNavigationModule(olModuleCalendar)
For Each objNavGroup In objNavMod.NavigationGroups
For Each objNavFolder In objNavGroup.NavigationFolders
If Not objNavFolder = "YOUR CALENDAR NAME" Then 'exactly as in Outlook
GoTo NxtGroup
End If
On Error Resume Next
Set objFolder = objNavFolder.Folder
NxtGroup:
Next
Next
MsgBox objFolder.Name
Set objNS = Nothing
Set objNavMod = Nothing
Set objNavGroup = Nothing
Set objNavFolder = Nothing
Set objFolder = Nothing
Set colExpl = Nothing
End Sub
添加 C# 版本的答案,因为最初的问题是针对 C# 提出的
Outlook.NameSpace mapiNamespace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
Outlook.CalendarModule calendarModule = (Outlook.CalendarModule)mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).GetExplorer().NavigationPane.Modules.GetNavigationModule(Outlook.OlNavigationModuleType.olModuleCalendar);
Outlook.NavigationGroups navGroups = calendarModule.NavigationGroups;
foreach (Outlook.NavigationGroup navGroup in navGroups)
{
Outlook.NavigationFolders navFolders = navGroup.NavigationFolders;
foreach (Outlook.NavigationFolder navFolder in navGroup.NavigationFolders)
{
try
{
if (navFolder.Folder != null && navFolder.Folder.Name.Equals("<Add your group calendar name here>"))
{
//do stuff here
return;
}
}
catch (Exception ex)
{
//handle exception as needed and iterate to next
continue;
}
}
}