-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutlookFormsProxy.cs
330 lines (311 loc) · 13 KB
/
OutlookFormsProxy.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
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace OutlookFormsManager
{
public static class OutlookFormsProxy
{
private static Outlook.Application m_Application = null;
private static Outlook.NameSpace m_NameSpace = null;
private static bool newOutlookInstance = false;
const string PR_COMMON_VIEWS_ENTRY_ID = "http://schemas.microsoft.com/mapi/proptag/0x35E60102";
const string SEARCH_FORM_MESSAGECLASS = "http://schemas.microsoft.com/mapi/proptag/0x6800001E";
const string PR_DISPLAY_NAME = "http://schemas.microsoft.com/mapi/proptag/0x3001001E";
const string PR_ENTRY_ID = "http://schemas.microsoft.com/mapi/proptag/0x0FFF0102";
const string PR_LONG_TERM_ENTRYID_FROM_TABLE = "http://schemas.microsoft.com/mapi/proptag/0x66700102";
const string prDefMsgClass = "http://schemas.microsoft.com/mapi/proptag/0x36E5001E";
const string prDefFormName = "http://schemas.microsoft.com/mapi/proptag/0x36E6001E";
private static void Initialise()
{
if (null == m_Application)
{
try
{
SecurityPermission secper = new SecurityPermission(PermissionState.Unrestricted);
secper.Assert();
object olApplicationInstance = null;
try
{
olApplicationInstance = Marshal.GetActiveObject("Outlook.Application");
}
catch (COMException comException)
{
if ((uint)comException.ErrorCode == 0x800401e3)
{
Console.WriteLine("No running instance of Outlook found.");
}
}
SecurityPermission.RevertAssert();
if (null != olApplicationInstance)
{
if (olApplicationInstance is Outlook.Application)
{
Console.WriteLine("Binding to existing Outlook instance.");
m_Application = olApplicationInstance as Outlook.Application;
}
}
else
{
Console.WriteLine("Creating a new Outlook instance.");
m_Application = new Outlook.Application();
newOutlookInstance = true;
}
if (null != m_Application)
{
m_NameSpace = m_Application.GetNamespace("MAPI");
}
else
{
Console.WriteLine("ERROR: Unable to start Outlook!");
}
}
catch (Exception exception)
{
Console.WriteLine("Unable to initialise application object. Error: " + exception.ToString());
}
}
}
public static void Exit()
{
if (null != m_Application)
{
m_NameSpace = null;
if (newOutlookInstance)
{
m_Application.Quit();
}
m_Application = null;
}
}
public static void ImportFormToPersonalFormsLibrary(string path, string name)
{
if (null == m_Application)
{
Initialise();
}
try
{
Outlook.FormDescription formDescription = null;
Outlook.MailItem mailItem = null;
Outlook.AppointmentItem appointmentItem = null;
dynamic formTemplate = m_Application.CreateItemFromTemplate(path);
if (formTemplate is Outlook.MailItem)
{
mailItem = formTemplate as Outlook.MailItem;
formDescription = mailItem.FormDescription;
}
else if (formTemplate is Outlook.AppointmentItem)
{
appointmentItem = formTemplate as Outlook.AppointmentItem;
formDescription = appointmentItem.FormDescription;
};
if (formDescription != null)
{
formDescription.Name = name;
formDescription.PublishForm(Outlook.OlFormRegistry.olPersonalRegistry);
ReleaseObject(formDescription);
formDescription = null;
}
if (mailItem != null)
{
ReleaseObject(mailItem);
}
if (appointmentItem != null)
{
ReleaseObject(appointmentItem);
}
}
catch (Exception exception)
{
Console.WriteLine("Unable to import custom form. Error: " + exception.ToString());
}
finally
{
}
}
public static void ReleaseObject(object obj)
{
Marshal.ReleaseComObject(obj);
}
private static Outlook.MAPIFolder GetCommonViewsFolder()
{
try
{
if (null == m_NameSpace)
{
Initialise();
}
Outlook.Store olStore = m_NameSpace.DefaultStore;
Outlook.PropertyAccessor olPropertyAccessor = olStore.PropertyAccessor;
string commonViewsEntryId = olPropertyAccessor.BinaryToString(olPropertyAccessor.GetProperty(PR_COMMON_VIEWS_ENTRY_ID));
if (commonViewsEntryId != string.Empty)
{
return m_NameSpace.GetFolderFromID(commonViewsEntryId);
}
return null;
}
catch (Exception exception)
{
Console.WriteLine("Unable to get common views folder. Error: " + exception.ToString());
return null;
}
}
public static void RemoveFormFromPersonalFormsLibrary(string name)
{
bool formFound = false;
Outlook.Table olTable;
Outlook.Row olRow;
string searchFilter;
if (null == m_Application)
{
Initialise();
}
try
{
Outlook.MAPIFolder olFolder = GetCommonViewsFolder();
if (null != olFolder)
{
searchFilter = "[MessageClass] = \"IPM.Microsoft.FolderDesign.FormsDescription\"";
olTable = olFolder.GetTable(searchFilter, Outlook.OlTableContents.olHiddenItems);
olTable.Columns.Add(PR_DISPLAY_NAME);
olTable.Columns.Add(SEARCH_FORM_MESSAGECLASS);
olTable.Columns.Add(PR_LONG_TERM_ENTRYID_FROM_TABLE);
olTable.Restrict(searchFilter);
while (!olTable.EndOfTable)
{
olRow = olTable.GetNextRow();
if (name.ToLower() == olRow[PR_DISPLAY_NAME].ToString().ToLower())
{
formFound = true;
byte[] entryId = olRow[PR_LONG_TERM_ENTRYID_FROM_TABLE];
string temp = "";
for (int i = 0; i < entryId.Length; i++)
{
temp += entryId[i].ToString("X2");
}
object item = m_NameSpace.GetItemFromID(temp, olFolder.StoreID);
if (item is Outlook.StorageItem)
{
Outlook.StorageItem storageItem = item as Outlook.StorageItem;
storageItem.Delete();
Console.WriteLine("Form succesfully deleted. You might need to restart Outlook.");
}
}
}
if (!formFound)
{
Console.WriteLine("The form couldn't be found in the Personal Forms Library.");
}
}
ClearLocalFormCache();
}
catch (Exception exception)
{
Console.WriteLine("Unable to remove form. Error: " + exception.ToString());
}
finally
{
}
}
public static void ClearLocalFormCache()
{
try
{
string LocalAppDataPath = Environment.ExpandEnvironmentVariables("%localappdata%");
string FormCacheDatPath = LocalAppDataPath + "\\Microsoft\\Forms\\FRMCACHE.DAT";
try
{
File.Delete(FormCacheDatPath);
Console.WriteLine("Local forms cache succesfully deleted.");
}
catch (Exception exception)
{
Console.WriteLine("Unable to delete local forms cache. Exception was: " + exception.ToString());
}
}
catch (Exception exception)
{
Console.WriteLine("Unable to clear local forms cache. Error: " + exception.ToString());
}
}
public static void SetDefaultInboxForm(string formName, string formClass)
{
if (null == m_NameSpace)
{
Initialise();
}
try
{
Outlook.MAPIFolder inboxFolder = m_NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
SetDefaultFormOnFolder(inboxFolder, formName, formClass);
Marshal.ReleaseComObject(inboxFolder);
}
catch (Exception exception)
{
Console.WriteLine("Unable to set default inbox form. Error: " + exception.ToString());
}
}
public static void SetDefaultCalendarForm(string formName, string formClass)
{
if (null == m_NameSpace)
{
Initialise();
}
try
{
Outlook.MAPIFolder calendarFolder = m_NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
SetDefaultFormOnFolder(calendarFolder, formName, formClass);
Marshal.ReleaseComObject(calendarFolder);
}
catch (Exception exception)
{
Console.WriteLine("Unable to set default calendar form. Error: " + exception.ToString());
}
}
public static void ResetDefaultForms()
{
if (null == m_NameSpace)
{
Initialise();
}
Outlook.MAPIFolder inboxFolder = null;
Outlook.MAPIFolder calendarFolder = null;
try
{
inboxFolder = m_NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
SetDefaultFormOnFolder(inboxFolder, "Message", "IPM.Note");
calendarFolder = m_NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
SetDefaultFormOnFolder(calendarFolder, "Appointment", "IPM.Appointment");
}
catch (Exception exception)
{
Console.WriteLine("Unable to set reset to default forms. Error: " + exception.ToString());
}
finally
{
if (inboxFolder != null)
ReleaseObject(inboxFolder);
if (calendarFolder != null)
ReleaseObject(calendarFolder);
}
}
public static void SetDefaultFormOnFolder(Outlook.MAPIFolder mapiFolder, string formName, string formClass)
{
string prDefMsgClass = "http://schemas.microsoft.com/mapi/proptag/0x36E5001E";
string prDefFormName = "http://schemas.microsoft.com/mapi/proptag/0x36E6001E";
try
{
Outlook.PropertyAccessor propertyAccessor = mapiFolder.PropertyAccessor;
propertyAccessor.SetProperty(prDefMsgClass, formClass);
propertyAccessor.SetProperty(prDefFormName, formName);
Marshal.ReleaseComObject(propertyAccessor);
}
catch (Exception exception)
{
Console.WriteLine("Unable to set the default form. Error: " + exception.ToString());
}
}
}
}