2013년 1월 24일 목요일

vsto outlook MAPIFolder getfolder

vsto outlook MAPIFolder getfolder



Util.subFolder(this.Application, "\\\\test1\\폴더99\\서브폴더99", "서브폴더999");



public static Microsoft.Office.Interop.Outlook.MAPIFolder GetMFolder(string folderPath, Microsoft.Office.Interop.Outlook.Folders folders)
        {
            //string dir = folderPath.Substring(0, folderPath.Substring(4).IndexOf("\\") + 4);
            string dir = folderPath.Substring(0, folderPath.Substring(7).IndexOf("\\") + 7); //\\test1 길이만큼 7
            try
            {
                foreach (Microsoft.Office.Interop.Outlook.MAPIFolder mf in folders)
                {
                    if (!(mf.FullFolderPath.StartsWith(dir))) continue;
                    if (mf.FullFolderPath == folderPath) return mf;
                    else
                    {
                        Microsoft.Office.Interop.Outlook.MAPIFolder temp = GetMFolder(folderPath, mf.Folders);
                        if (temp != null)
                            return temp;
                    }
                }
                return null;
            }
            catch { return null; }
        }



//서브 폴더만들기
        public static bool subFolder(Outlook.Application application, string targetfolder, string foldername)
        {
            bool ret = false;
            Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
            Microsoft.Office.Interop.Outlook.Folder fShareFolder = getFolder(nameSpace.Folders, "test1");
            Outlook.MAPIFolder folderInbox = fShareFolder;

            //Outlook.Folders inboxFolders = folderInbox.Folders;
            Microsoft.Office.Interop.Outlook._Folders oFolders;
            oFolders = folderInbox.Folders;
            Microsoft.Office.Interop.Outlook.MAPIFolder findFolder = null;
            findFolder = GetMFolder(targetfolder, fShareFolder.Folders);          
            Outlook.Folders inboxFolders = findFolder.Folders;
            Outlook.MAPIFolder subfolderInbox = null;

            try
            {
                subfolderInbox = inboxFolders.Add(foldername, Outlook.OlDefaultFolders.olFolderInbox);
                ret = true;
            }
            catch (COMException exception)
            {
                //if (exception.ErrorCode == -2147352567)
                //    //  Cannot create the folder.
                //    System.Windows.Forms.MessageBox.Show(exception.Message);
            }
            if (subfolderInbox != null) Marshal.ReleaseComObject(subfolderInbox);
            if (inboxFolders != null) Marshal.ReleaseComObject(inboxFolders);
            if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
            if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
            return ret;
        }




private MAPIFolder GetFolder(string folderPath, Folders folders)
{
    string dir = folderPath.Substring(0, folderPath.Substring(4).IndexOf("\\") + 4);
    try
    {
        foreach (MAPIFolder mf in folders)
        {
            if (!(mf.FullFolderPath.StartsWith(dir))) continue;
            if (mf.FullFolderPath == folderPath) return mf;
            else
            {
                MAPIFolder temp = GetFolder(folderPath, mf.Folders);
                if (temp != null)
                    return temp;
            }
        }
        return null;
    }
    catch { return null; }
}

댓글 없음: