[C#/COMMON/OUTLOOK] 폴더/메일 항목 사용자 속성 추가/조회/삭제하기
■ 폴더/메일 항목에 사용자 속성을 추가/조회/삭제하는 방법을 보여준다. ▶ 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