■ 폴더/메일 항목에 사용자 속성을 추가/조회/삭제하는 방법을 보여준다.
▶ PictureConverter.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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
using System; using System.Drawing; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// PICTURE 변환자 /// </summary> public static class PictureConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Class ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 그림 설명 - PICTDESC /// <summary> /// 그림 설명 /// </summary> private static class PICTDESC { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// PICTYPE_UNINITIALIZED /// </summary> public const short PICTYPE_UNINITIALIZED = -1; /// <summary> /// PICTYPE_NONE /// </summary> public const short PICTYPE_NONE = 0; /// <summary> /// PICTYPE_BITMAP /// </summary> public const short PICTYPE_BITMAP = 1; /// <summary> /// PICTYPE_METAFILE /// </summary> public const short PICTYPE_METAFILE = 2; /// <summary> /// PICTYPE_ICON /// </summary> public const short PICTYPE_ICON = 3; /// <summary> /// PICTYPE_ENHMETAFILE /// </summary> public const short PICTYPE_ENHMETAFILE = 4; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Class ////////////////////////////////////////////////////////////////////////////////////////// Public #region 아이콘 - Icno /// <summary> /// 아이콘 /// </summary> [StructLayout(LayoutKind.Sequential)] public class Icon { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 크기 /// </summary> public int Size = Marshal.SizeOf(typeof(PICTDESC.Icon)); /// <summary> /// 그림 타입 /// </summary> public int PictureType = PICTDESC.PICTYPE_ICON; /// <summary> /// 아이콘 핸들 /// </summary> public IntPtr IconHandle = IntPtr.Zero; /// <summary> /// 미사용 1 /// </summary> public int Unused1 = 0; /// <summary> /// 미사용 2 /// </summary> public int Unused2 = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Icon(icon) /// <summary> /// 생성자 /// </summary> /// <param name="icon">아이콘</param> public Icon(System.Drawing.Icon icon) { IconHandle = icon.ToBitmap().GetHicon(); } #endregion } #endregion #region 비트맵 - Bitmap /// <summary> /// 비트맵 /// </summary> [StructLayout(LayoutKind.Sequential)] public class Bitmap { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 크기 /// </summary> public int Size = Marshal.SizeOf(typeof(PICTDESC.Bitmap)); /// <summary> /// 그림 타입 /// </summary> public int PictureType = PICTDESC.PICTYPE_BITMAP; /// <summary> /// 비트맵 핸들 /// </summary> public IntPtr BitmapHandle = IntPtr.Zero; /// <summary> /// 팔레트 핸들 /// </summary> public IntPtr PaletteHandle = IntPtr.Zero; /// <summary> /// 미사용 /// </summary> public int Unused = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Bitmap(bitmap) /// <summary> /// 생성자 /// </summary> public Bitmap(System.Drawing.Bitmap bitmap) { BitmapHandle = bitmap.GetHbitmap(); } #endregion } #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region OLE 그림 생성하기 (간접) - OleCreatePictureIndirect(pictureDescription, interfaceID, own); /// <summary> /// OLE 그림 생성하기 (간접) /// </summary> /// <param name="pictureDescription">그림 설명</param> /// <param name="interfaceID">인터페이스 ID</param> /// <param name="own">소유 여부</param> /// <returns>IPictureDisp 인터페이스 객체</returns> [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true, PreserveSig = false)] private static extern stdole.IPictureDisp OleCreatePictureIndirect ( [MarshalAs(UnmanagedType.AsAny)] object pictureDescription, ref Guid interfaceID, bool own ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// IPictureDisp 인터페이스 GUID /// </summary> private static Guid _iPictureDispGUID = typeof(stdole.IPictureDisp).GUID; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region IPictureDisp 인터페이스 객체 구하기 - ToPicture(icon) /// <summary> /// IPictureDisp 인터페이스 객체 구하기 /// </summary> /// <param name="icon">아이콘</param> /// <returns>IPictureDisp 인터페이스 객체</returns> public static stdole.IPictureDisp ToPicture(Icon icon) { PICTDESC.Icon pictDescIcon = new PICTDESC.Icon(icon); return OleCreatePictureIndirect(pictDescIcon, ref _iPictureDispGUID, true); } #endregion #region IPictureDisp 인터페이스 객체 구하기 - ToPicture(image) /// <summary> /// IPictureDisp 인터페이스 객체 구하기 /// </summary> /// <param name="image">이미지</param> /// <returns>IPictureDisp 인터페이스 객체</returns> public static stdole.IPictureDisp ToPicture(Image image) { Bitmap bitmap = (image is Bitmap) ? (Bitmap)image : new Bitmap(image); PICTDESC.Bitmap pictDescBitmap = new PICTDESC.Bitmap(bitmap); return OleCreatePictureIndirect(pictDescBitmap, ref _iPictureDispGUID, true); } #endregion } } |
▶ FolderExtension.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 |
using Microsoft.Office.Interop.Outlook; using System.Drawing; namespace TestProject { /// <summary> /// 폴더 확장 /// </summary> public static class FolderExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 속성 스키마 헤더 /// </summary> private static string _propertySchemaHeader = "http://schemas.microsoft.com/mapi/string/{C18E58E4-1F09-44BD-85AA-D5E2641EBA4D}/"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 사용자 속성 값 설정하기 - SetUserPropertyValue(folder, propertyName, propertyValue) /// <summary> /// 사용자 속성 값 설정하기 /// </summary> /// <param name="folder">폴더</param> /// <param name="propertyName">속성명</param> /// <param name="propertyValue">속성값</param> public static void SetUserPropertyValue(this Folder folder, string propertyName, object propertyValue) { folder?.PropertyAccessor?.SetProperty($"{_propertySchemaHeader}{propertyName}", propertyValue); } #endregion #region 사용자 속성 값 구하기 - GetUserPropertyValue(folder, propertyName) /// <summary> /// 사용자 속성 값 구하기 /// </summary> /// <param name="folder">폴더</param> /// <param name="propertyName">속성명</param> /// <returns>사용자 속성 값</returns> public static object GetUserPropertyValue(this Folder folder, string propertyName) { return folder?.PropertyAccessor?.GetProperty($"{_propertySchemaHeader}{propertyName}"); } #endregion #region 사용자 속성 값 소유 여부 구하기 - HasUserPropertyValue(folder, propertyName) /// <summary> /// 사용자 속성 값 소유 여부 구하기 /// </summary> /// <param name="folder">폴더</param> /// <param name="propertyName">속성명</param> /// <returns>사용자 속성 값 소유 여부</returns> public static bool HasUserPropertyValue(this Folder folder, string propertyName) { try { return GetUserPropertyValue(folder, propertyName) != null ? true : false; } catch(System.Exception) { return false; } } #endregion #region 사용자 속성 삭제하기 - DeleteUserProperty(folder, propertyName) /// <summary> /// 사용자 속성 삭제하기 /// </summary> /// <param name="folder">폴더</param> /// <param name="propertyName">속성명</param> public static void DeleteUserProperty(this Folder folder, string propertyName) { folder?.PropertyAccessor?.DeleteProperty($"{_propertySchemaHeader}{propertyName}"); } #endregion #region 아이콘 설정하기 - SetIcon(folder, bitmap) /// <summary> /// 아이콘 설정하기 /// </summary> /// <param name="folder">폴더</param> /// <param name="iconBitmap">비트맵</param> public static void SetIcon(this Folder folder, Bitmap bitmap) { stdole.StdPicture picture = PictureConverter.ToPicture(bitmap) as stdole.StdPicture; folder.SetCustomIcon(picture); } #endregion #region 아이콘 설정하기 - SetIcon(folder, icon) /// <summary> /// 아이콘 설정하기 /// </summary> /// <param name="folder">폴더</param> /// <param name="icon">아이콘</param> public static void SetIcon(this Folder folder, Icon icon) { stdole.StdPicture picture = PictureConverter.ToPicture(icon) as stdole.StdPicture; folder.SetCustomIcon(picture); } #endregion #region 아이콘 삭제하기 - DeleteIcon(folder) /// <summary> /// 아이콘 삭제하기 /// </summary> /// <param name="folder">폴더</param> public static void DeleteIcon(this Folder folder) { folder.SetCustomIcon(null); } #endregion } } |
▶ MailItemExtension.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 |
using Microsoft.Office.Interop.Outlook; namespace TestProject { /// <summary> /// 메일 항목 확장 /// </summary> public static class MailItemExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 속성 스키마 헤더 /// </summary> private static string _propertySchemaHeader = "http://schemas.microsoft.com/mapi/string/{2ECFB886-7542-463C-8D56-0F9C29D02368}/"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 사용자 속성 값 설정하기 - SetUserPropertyValue(mailItem, propertyName, propertyValue) /// <summary> /// 사용자 속성 값 설정하기 /// </summary> /// <param name="mailItem">메일 항목</param> /// <param name="propertyName">속성명</param> /// <param name="propertyValue">속성값</param> public static void SetUserPropertyValue(this MailItem mailItem, string propertyName, object propertyValue) { mailItem?.PropertyAccessor?.SetProperty($"{_propertySchemaHeader}{propertyName}", propertyValue); } #endregion #region 사용자 속성 값 구하기 - GetUserPropertyValue(mailItem, propertyName) /// <summary> /// 사용자 속성 값 구하기 /// </summary> /// <param name="mailItem">메일 항목</param> /// <param name="propertyName">속성명</param> /// <returns>사용자 속성 값</returns> public static object GetUserPropertyValue(this MailItem mailItem, string propertyName) { return mailItem?.PropertyAccessor?.GetProperty($"{_propertySchemaHeader}{propertyName}"); } #endregion #region 사용자 속성 값 소유 여부 구하기 - HasUserPropertyValue(mailItem, propertyName) /// <summary> /// 사용자 속성 값 소유 여부 구하기 /// </summary> /// <param name="mailItem">메일 항목</param> /// <param name="propertyName">속성명</param> /// <returns>사용자 속성 값 소유 여부</returns> public static bool HasUserPropertyValue(this MailItem mailItem, string propertyName) { try { return GetUserPropertyValue(mailItem, propertyName) != null ? true : false; } catch(System.Exception) { return false; } } #endregion #region 사용자 속성 삭제하기 - DeleteUserProperty(mailItem, propertyName) /// <summary> /// 사용자 속성 삭제하기 /// </summary> /// <param name="mailItem">메일 항목</param> /// <param name="propertyName">속성명</param> public static void DeleteUserProperty(this MailItem mailItem, string propertyName) { mailItem?.PropertyAccessor?.DeleteProperty($"{_propertySchemaHeader}{propertyName}"); } #endregion #region 아이콘 설정하기 - SetIcon(mailItem, iconIndex) /// <summary> /// 아이콘 설정하기 /// </summary> /// <param name="mailItem">메일 항목</param> /// <param name="iconIndex">아이콘 인덱스</param> public static void SetIcon(this MailItem mailItem, int iconIndex) { mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", iconIndex); mailItem.Save(); } #endregion #region 아이콘 삭제하기 - DeleteIcon(mailItem) /// <summary> /// 아이콘 삭제하기 /// </summary> /// <param name="mailItem">메일 항목</param> public static void DeleteIcon(this MailItem mailItem) { SetIcon(mailItem, 315); } #endregion #region 체크 아이콘 설정하기 - SetCheckIcon(mailItem) /// <summary> /// 체크 아이콘 설정하기 /// </summary> /// <param name="mailItem">메일 항목</param> public static void SetCheckIcon(this MailItem mailItem) { SetIcon(mailItem, 311); } #endregion } } |
▶ RibbonContextMenu.xml
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 |
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="customUI_onLoad"> <contextMenus> <contextMenu idMso="ContextMenuFolder"> <button id="setFolderUserPropertyValueButton" label="사용자 속성 값 설정하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> <button id="getFolderUserPropertyValueButton" label="사용자 속성 값 구하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> <button id="deleteFolderUserPropertyButton" label="사용자 속성 삭제하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> </contextMenu> <contextMenu idMso="ContextMenuMailItem"> <button id="setMailItemUserPropertyValueButton" label="사용자 속성 값 설정하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> <button id="getMailItemUserPropertyValueButton" label="사용자 속성 값 구하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> <button id="deleteMailItemUserPropertyButton" label="사용자 속성 삭제하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> </contextMenu> <contextMenu idMso="ContextMenuMultipleItems"> <button id="setMailItemUserPropertyValuesButton" label="사용자 속성 값 설정하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> <button id="getMailItemUserPropertyValuesButton" label="사용자 속성 값 구하기" getVisible="contextMenu_getVisible" onAction="contextMenu_onAction" /> <button id="deleteMailItemUserPropertiesButton" label="사용자 속성 삭제하기" getVisible="contextMenu_getVisible" onAction="contextMenu_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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
using Microsoft.Office.Core; using Microsoft.Office.Interop.Outlook; using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { /// <summary> /// 리본 컨텍스트 메뉴 /// </summary> [ComVisible(true)] public class RibbonContextMenu : IRibbonExtensibility { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 리본 UI /// </summary> private IRibbonUI ribbonUI; /// <summary> /// 폴더 속성명 /// </summary> private static readonly string FolderPropertyName = "36CF838B-FDBD-4F02-87E4-6008965A9B35"; /// <summary> /// 메일 항목 속성명 /// </summary> private static readonly string MailItemPropertyName = "8D161D16-C1A8-4990-A9F6-700FF14A8F40"; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - RibbonContextMenu() /// <summary> /// 생성자 /// </summary> public RibbonContextMenu() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public //////////////////////////////////////////////////////////////////////////////// Event #region 커스텀 UI 로드시 처리하기 - customUI_onLoad(ribbonUI) /// <summary> /// 커스텀 UI 로드시 처리하기 /// </summary> /// <param name="ribbonUI">리본 UI</param> public void customUI_onLoad(IRibbonUI ribbonUI) { this.ribbonUI = ribbonUI; } #endregion #region 컨텍스트 메뉴 표시 여부 구하기 - contextMenu_getVisible(ribbonControl) /// <summary> /// 컨텍스트 메뉴 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>컨텍스트 메뉴 표시 여부</returns> public bool contextMenu_getVisible(IRibbonControl ribbonControl) { switch(ribbonControl.Id) { case "setFolderUserPropertyValueButton" : return GetSetFolderUserPropertyValueButtonVisible(ribbonControl); case "getFolderUserPropertyValueButton" : return GetGetFolderUserPropertyValueButtonVisible(ribbonControl); case "deleteFolderUserPropertyButton" : return GetDeleteFolderUserPropertyButtonVisible (ribbonControl); case "setMailItemUserPropertyValueButton" : return GetSetMailItemUserPropertyValueButtonVisible(ribbonControl); case "getMailItemUserPropertyValueButton" : return GetGetMailItemUserPropertyValueButtonVisible(ribbonControl); case "deleteMailItemUserPropertyButton" : return GetDeleteMailItemUserPropertyButtonVisible (ribbonControl); case "setMailItemUserPropertyValuesButton" : return GetSetMailItemUserPropertyValueButtonVisible(ribbonControl); case "getMailItemUserPropertyValuesButton" : return GetGetMailItemUserPropertyValueButtonVisible(ribbonControl); case "deleteMailItemUserPropertiesButton" : return GetDeleteMailItemUserPropertyButtonVisible (ribbonControl); default : return false; } } #endregion #region 컨텍스트 메뉴 작업시 처리하기 - contextMenu_onAction(ribbonControl) /// <summary> /// 컨텍스트 메뉴 작업시 처리하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> public void contextMenu_onAction(IRibbonControl ribbonControl) { switch(ribbonControl.Id) { case "setFolderUserPropertyValueButton" : SetFolderUserPropertyValue(ribbonControl); break; case "getFolderUserPropertyValueButton" : GetFolderUserPropertyValue(ribbonControl); break; case "deleteFolderUserPropertyButton" : DeleteFolderUserProperty (ribbonControl); break; case "setMailItemUserPropertyValueButton" : SetMailItemUserPropertyValue(ribbonControl); break; case "getMailItemUserPropertyValueButton" : GetMailItemUserPropertyValue(ribbonControl); break; case "deleteMailItemUserPropertyButton" : DeleteMailItemUserProperty (ribbonControl); break; case "setMailItemUserPropertyValuesButton" : SetMailItemUserPropertyValue(ribbonControl); break; case "getMailItemUserPropertyValuesButton" : GetMailItemUserPropertyValue(ribbonControl); break; case "deleteMailItemUserPropertiesButton" : DeleteMailItemUserProperty (ribbonControl); break; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region (IRibbonExtensibility) 커스텀 UI 구하기 - GetCustomUI(ribbonID) public string GetCustomUI(string ribbonID) { return ribbonID.Equals("Microsoft.Outlook.Explorer") ? GetResourceText("TestProject.RibbonContextMenu.xml") : null; } #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(); int count = resourceNameArray.Length; for(int i = 0; i < count; ++i) { if(string.Compare(resourceName, resourceNameArray[i], StringComparison.OrdinalIgnoreCase) == 0) { using(StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(resourceNameArray[i]))) { if(reader != null) { return reader.ReadToEnd(); } } } } return null; } #endregion #region 폴더 사용자 속성 값 설정하기 버튼 표시 여부 구하기 - GetSetFolderUserPropertyValueButtonVisible(ribbonControl) /// <summary> /// 폴더 사용자 속성 값 설정하기 버튼 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>폴더 사용자 속성 값 설정하기 버튼 표시 여부</returns> private bool GetSetFolderUserPropertyValueButtonVisible(IRibbonControl ribbonControl) { try { Folder folder = ribbonControl.Context as Folder; return !folder.HasUserPropertyValue(FolderPropertyName); } catch { return false; } } #endregion #region 폴더 사용사 속성 값 구하기 버튼 표시 여부 구하기 - GetGetFolderUserPropertyValueButtonVisible(ribbonControl) /// <summary> /// 폴더 사용사 속성 값 구하기 버튼 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>폴더 사용사 속성 값 구하기 버튼 표시 여부</returns> private bool GetGetFolderUserPropertyValueButtonVisible(IRibbonControl ribbonControl) { try { Folder folder = ribbonControl.Context as Folder; return folder.HasUserPropertyValue(FolderPropertyName); } catch { return false; } } #endregion #region 폴더 사용자 속성 제거하기 버튼 표시 여부 구하기 - GetDeleteFolderUserPropertyButtonVisible(ribbonControl) /// <summary> /// 폴더 사용자 속성 제거하기 버튼 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>폴더 사용자 속성 제거하기 버튼 표시 여부</returns> private bool GetDeleteFolderUserPropertyButtonVisible(IRibbonControl ribbonControl) { try { Folder folder = ribbonControl.Context as Folder; return folder.HasUserPropertyValue(FolderPropertyName); } catch { return false; } } #endregion #region 메일 항목 사용자 속성 값 설정하기 버튼 표시 여부 구하기 - GetSetMailItemUserPropertyValueButtonVisible(ribbonControl) /// <summary> /// 메일 항목 사용자 속성 값 설정하기 버튼 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>메일 항목 사용자 속성 값 설정하기 버튼 표시 여부</returns> private bool GetSetMailItemUserPropertyValueButtonVisible(IRibbonControl ribbonControl) { try { dynamic items = ribbonControl.Context; foreach(dynamic item in items) { if(item is MailItem mailItem) { if(!mailItem.HasUserPropertyValue(MailItemPropertyName)) { return true; } } } return false; } catch(System.Exception exception) { return false; } } #endregion #region 메일 항목 사용자 속성 값 구하기 버튼 표시 여부 구하기 - GetGetMailItemUserPropertyValueButtonVisible(ribbonControl) /// <summary> /// 메일 항목 사용자 속성 값 구하기 버튼 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>메일 항목 사용자 속성 값 구하기 버튼 표시 여부</returns> private bool GetGetMailItemUserPropertyValueButtonVisible(IRibbonControl ribbonControl) { try { dynamic items = ribbonControl.Context; foreach(dynamic item in items) { if(item is MailItem mailItem) { if(mailItem.HasUserPropertyValue(MailItemPropertyName)) { return true; } } } return false; } catch { return false; } } #endregion #region 메일 항목 사용자 속성 삭제하기 버튼 표시 여부 구하기 - GetDeleteMailItemUserPropertyButtonVisible(ribbonControl) /// <summary> /// 메일 항목 사용자 속성 삭제하기 버튼 표시 여부 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> /// <returns>메일 항목 사용자 속성 삭제하기 버튼 표시 여부</returns> private bool GetDeleteMailItemUserPropertyButtonVisible(IRibbonControl ribbonControl) { try { dynamic items = ribbonControl.Context; foreach(dynamic item in items) { if(item is MailItem mailItem) { if(mailItem.HasUserPropertyValue(MailItemPropertyName)) { return true; } } } return false; } catch { return false; } } #endregion #region 폴더 사용자 속성 값 설정하기 - SetFolderUserPropertyValue(ribbonControl) /// <summary> /// 폴더 사용자 속성 값 설정하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> private void SetFolderUserPropertyValue(IRibbonControl ribbonControl) { try { Folder folder = ribbonControl.Context as Folder; string propertyValue = DateTime.Now.ToString(); folder?.SetUserPropertyValue(FolderPropertyName, propertyValue); folder.SetIcon(Properties.Resources.check); MessageBox.Show($"사용자 속성 값을 설정하였습니다.\r\n폴더 : {folder.Name}\r\n사용자 속성 값 : {propertyValue}"); } catch(System.Exception exception) { MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } #endregion #region 폴더 사용자 속성 값 구하기 - GetFolderUserPropertyValue(ribbonControl) /// <summary> /// 폴더 사용자 속성 값 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> private void GetFolderUserPropertyValue(IRibbonControl ribbonControl) { try { Folder folder = ribbonControl.Context as Folder; object propertyValue = folder?.GetUserPropertyValue(FolderPropertyName); MessageBox.Show($"사용자 속성 값을 구합니다.\r\n폴더 : {folder.Name}\r\n사용자 속성 값 : {propertyValue}"); } catch(System.Exception exception) { MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } #endregion #region 폴더 사용자 속성 제거하기 - DeleteFolderUserProperty(ribbonControl) /// <summary> /// 폴더 사용자 속성 제거하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> private void DeleteFolderUserProperty(IRibbonControl ribbonControl) { try { Folder folder = ribbonControl.Context as Folder; folder.DeleteUserProperty(FolderPropertyName); folder.DeleteIcon(); MessageBox.Show($"사용자 속성을 삭제했습니다.\r\n폴더 : {folder.Name}"); } catch(System.Exception exception) { MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } #endregion #region 메일 항목 사용자 속성 값 설정하기 - SetMailItemUserPropertyValue(ribbonControl) /// <summary> /// 메일 항목 사용자 속성 값 설정하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> private void SetMailItemUserPropertyValue(IRibbonControl ribbonControl) { try { dynamic items = ribbonControl.Context; foreach(dynamic item in items) { if(item is MailItem mailItem) { string propertyValue = DateTime.Now.ToString(); mailItem.SetUserPropertyValue(MailItemPropertyName, propertyValue); mailItem.SetCheckIcon(); MessageBox.Show($"사용자 속성 값을 설정하였습니다.\r\n메일 항목 : {mailItem.Subject.Substring(0, 20)}\r\n사용자 속성 값 : {propertyValue}"); } } } catch(System.Exception exception) { MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } #endregion #region 메일 항목 사용자 속성 값 구하기 - GetMailItemUserPropertyValue(ribbonControl) /// <summary> /// 메일 항목 사용자 속성 값 구하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> private void GetMailItemUserPropertyValue(IRibbonControl ribbonControl) { try { dynamic items = ribbonControl.Context; foreach(dynamic item in items) { if(item is MailItem mailItem) { if(mailItem.HasUserPropertyValue(MailItemPropertyName)) { object propertyValue = mailItem.GetUserPropertyValue(MailItemPropertyName); MessageBox.Show($"사용자 속성 값을 구합니다.\r\n메일 항목 : {mailItem.Subject}\r\n사용자 속성 값 : {propertyValue}"); } } } } catch(System.Exception exception) { MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } #endregion #region 메일 항목 사용자 속성 삭제하기 - DeleteMailItemUserProperty(ribbonControl) /// <summary> /// 메일 항목 사용자 속성 삭제하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> private void DeleteMailItemUserProperty(IRibbonControl ribbonControl) { try { dynamic items = ribbonControl.Context; foreach(dynamic item in items) { if(item is MailItem mailItem) { if(mailItem.HasUserPropertyValue(MailItemPropertyName)) { mailItem.DeleteUserProperty(MailItemPropertyName); mailItem.DeleteIcon(); MessageBox.Show($"사용자 속성을 삭제했습니다.\r\n메일 항목 : {mailItem.Subject}"); } } } } catch(System.Exception exception) { MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } } #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 EventHandler(CustomAddIn_Startup ); this.Shutdown += new EventHandler(CustomAddIn_Shutdown); } #endregion } } |