我正在使用以下代码为outlook创建AppointmentItem
个对象:
AppointmentItem apt = (APPointmentItem)OLApp.CreateItem(OlItemType.olAppointmentItem);
// set parameters for 'apt', like body, subject etc.
// ...
apt.Save();
我有一个日历的名称,我想把这个事件放进去,但我无法弄清楚如何指定新创建的事件应该进入哪个文件夹。新事件似乎总是出现在主日历文件夹中。
答案 0 :(得分:2)
找到解决方案。感谢Eugene Astafiev在http://www.add-in-express.com/creating-addins-blog/2011/11/04/outlook-create-appointment-item/
提供所以上面的代码应该是:
using Microsoft.Office.Interop.Outlook;
Application outlookApplication = new Application();
MAPIFolder customer_folder = GetMyFolder(path, outlookApplication.Session.Folders); //function to get your folder
AppointmentItem apt = (AppointmentItem)customer_folder.Items.Add(OlItemType.olAppointmentItem);
// set parameters for 'apt', like body, subject etc.
// ...
apt.Save();
以下是GetMyFolder
递归查找自定义文件夹的代码:
using System.Collections;
using System.Linq;
using Microsoft.Office.Interop.Outlook;
private MAPIFolder _mapiFolder;
private MAPIFolder GetMyFolder(string path, IEnumerable folders)
{
if (!path.StartsWith(@"\\", StringComparison.Ordinal))
return null;
string pathRoot = GetFolderPathRoot(path);
foreach (Folder folder in folders.Cast<Folder>().TakeWhile(
folder => _mapiFolder == null).Select(
folder => new { folder, folderRoot = GetFolderPathRoot(folder.FolderPath) }).Where(
folder => folder.folderRoot == pathRoot).Select(folder => folder.folder))
{
if (folder.DefaultItemType == OlItemType.olAppointmentItem && folder.FolderPath == path)
{
s_mapiFolder = folder;
break;
}
if (folder.Folders.Cast<Folder>().Any())
GetMapiFolder(false, folder.Folders, path);
}
return _mapiFolder;
}
private static string GetFolderPathRoot(string folderPath)
{
// Strip header directory seperator characters
folderPath = folderPath.Remove(0, 2);
// Find the index of a directory seperator character
int index = folderPath.IndexOf(Path.DirectorySeparatorChar, 0);
// Reconstruct the root path according to the index found
return String.Format(@"\\{0}", index > 0 ? folderPath.Substring(0, index) : folderPath);
}
编辑:修改后的代码,只能在指定路径根目录下的文件夹中进行递归搜索。
答案 1 :(得分:1)
您需要做的是访问该文件夹,然后调用folder.items.add并添加项目。看起来应该是这样的:
Microsoft.Office.Interop.Outlook.MAPIFolder customer_folder = GetMyFolder(); //function to get your folder
AppointmentItem apt = (APPointmentItem)OLApp.CreateItem(OlItemType.olAppointmentItem);
// set parameters for 'apt', like body, subject etc.
// ...
apt.Save();
customer_folder.Items.Add(apt);