■ MAPIFolder 인터페이스의 PropertyAccessor 속성을 사용해 사용자 속성을 추가/조회/삭제하는 방법을 보여준다.
▶ RibbonContextMenu.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="customUI_Load"> <contextMenus> <contextMenu idMso="ContextMenuFolder"> <button id="setFolderUserPropertyContextMenu" label="폴더 사용자 속성 설정하기" getVisible="folderContextMenu_getVisible" onAction="folderContextMenu_onAction" /> <button id="getFolderUserPropertyContextMenu" label="폴더 사용자 속성 가져오기" getVisible="folderContextMenu_getVisible" onAction="folderContextMenu_onAction" /> <button id="deleteFolderUserPropertyContextMenu" label="폴더 사용자 속성 제거하기" getVisible="folderContextMenu_getVisible" onAction="folderContextMenu_onAction" /> </contextMenu> </contextMenus> </customUI> |
▶ RibbonContextMenu.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
using Microsoft.Office.Core; using Microsoft.Office.Interop.Outlook; using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 리본 컨텍스트 메뉴 /// </summary> [ComVisible(true)] public class RibbonContextMenu : IRibbonExtensibility { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 리본 UID /// </summary> private IRibbonUI ribbonUI; /// <summary> /// 폴더 속성 스키마입니다. /// </summary> private string folderPropertySchema = "http://schemas.microsoft.com/mapi/string/{F0974C06-6AD7-4ADF-B334-78C4B3B15B87}/CUSTOM-TAG"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - RibbonContextMenu() /// <summary> /// 생성자 /// </summary> public RibbonContextMenu() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public //////////////////////////////////////////////////////////////////////////////// Event #region 커스텀 UI 로드시 처리하기 - customUI_Load(ribbonUI) /// <summary> /// 커스텀 UI 로드시 처리하기 /// </summary> /// <param name="ribbonUI">리본 UI</param> public void customUI_Load(IRibbonUI ribbonUI) { this.ribbonUI = ribbonUI; } #endregion #region 폴더 컨텍스트 메뉴 표시 여부 구하기 - folderContextMenu_getVisible(ribbonControl) /// <summary> /// 폴더 컨텍스트 메뉴 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>처리 결과</returns> public bool folderContextMenu_getVisible(IRibbonControl ribbonControl) { string id = ribbonControl.Id; if(id == "setFolderUserPropertyContextMenu") { try { Folder folder = ribbonControl.Context as Folder; PropertyAccessor propertyAccessor = folder?.PropertyAccessor; dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema); } catch { return true; } return false; } else if(id == "getFolderUserPropertyContextMenu" || id == "deleteFolderUserPropertyContextMenu") { try { Folder folder = ribbonControl.Context as Folder; PropertyAccessor propertyAccessor = folder?.PropertyAccessor; dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema); } catch { return false; } return true; } return false; } #endregion #region 폴더 컨텍스트 메뉴 작업시 처리하기 - folderContextMenu_onAction(ribbonControl) /// <summary> /// 폴더 컨텍스트 메뉴 작업시 처리하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> public void folderContextMenu_onAction(IRibbonControl ribbonControl) { string id = ribbonControl.Id; if(id == "setFolderUserPropertyContextMenu") { try { Folder folder = ribbonControl.Context as Folder; PropertyAccessor propertyAccessor = folder?.PropertyAccessor; propertyAccessor?.SetProperty(folderPropertySchema, DateTime.Now.ToString()); dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema); System.Windows.Forms.MessageBox.Show($"사용자 속성을 설정하였습니다.\r\n폴더 : {folder.Name}\r\n사용자 속성값 : {propertyValue}"); } catch(System.Exception exception) { System.Windows.Forms.MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } else if(id == "getFolderUserPropertyContextMenu") { try { Folder folder = ribbonControl.Context as Folder; PropertyAccessor propertyAccessor = folder?.PropertyAccessor; dynamic propertyValue = propertyAccessor?.GetProperty(folderPropertySchema); System.Windows.Forms.MessageBox.Show($"사용자 속성을 가져왔습니다.\r\n폴더 : {folder.Name}\r\n사용자 속성값 : {propertyValue}"); } catch(System.Exception exception) { System.Windows.Forms.MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } else if(id == "deleteFolderUserPropertyContextMenu") { try { Folder folder = ribbonControl.Context as Folder; PropertyAccessor propertyAccessor = folder?.PropertyAccessor; propertyAccessor?.DeleteProperty(folderPropertySchema); System.Windows.Forms.MessageBox.Show($"사용자 속성을 제거하였습니다.\r\n폴더 : {folder.Name}"); } catch(System.Exception exception) { System.Windows.Forms.MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region (IRibbonExtensibility) 커스텀 UI 구하기 - GetCustomUI(ribbonID) /// <summary> /// 커스텀 UI 구하기 /// </summary> /// <param name="ribbonID"></param> /// <returns>커스텀 UI</returns> public string GetCustomUI(string ribbonID) { return GetResourceText("TestProject.RibbonContextMenu.xml"); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Function #region 리소스 텍스트 구하기 - GetResourceText(resourceName) /// <summary> /// 리소스 텍스트 구하기 /// </summary> /// <param name="resourceName">리소스명</param> /// <returns>리소스 텍스트</returns> private static string GetResourceText(string resourceName) { Assembly assembly = Assembly.GetExecutingAssembly(); string[] resourceNameArray = assembly.GetManifestResourceNames(); for(int i = 0; i < resourceNameArray.Length; ++i) { if(string.Compare(resourceName, resourceNameArray[i], StringComparison.OrdinalIgnoreCase) == 0) { using(StreamReader resourceReader = new StreamReader(assembly.GetManifestResourceStream(resourceNameArray[i]))) { if(resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #endregion } } |
▶ CustomAddIn.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using Microsoft.Office.Core; using System; namespace TestProject { /// <summary> /// 커스텀 애드인 /// </summary> public partial class CustomAddIn { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 리본 확장성 객체 생성하기 - CreateRibbonExtensibilityObject() /// <summary> /// 리본 확장성 객체 생성하기 /// </summary> /// <returns>리본 확장성 인터페이스 객체</returns> protected override IRibbonExtensibility CreateRibbonExtensibilityObject() { return new RibbonContextMenu(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 커스텀 애드인 시작시 처리하기 - CustomAddIn_Startup(sender, e) /// <summary> /// 커스텀 애드인 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CustomAddIn_Startup(object sender, EventArgs e) { } #endregion #region 커스텀 애드인 셧다운시 처리하기 - CustomAddIn_Shutdown(sender, e) /// <summary> /// 커스텀 애드인 셧다운시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CustomAddIn_Shutdown(object sender, EventArgs e) { } #endregion #region VSTO에서 생성한 코드 /// <summary> /// 디자이너 지원에 필요한 메서드입니다. /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(CustomAddIn_Startup); this.Shutdown += new System.EventHandler(CustomAddIn_Shutdown); } #endregion } } |