-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThisAddIn.cs
More file actions
206 lines (174 loc) · 6.85 KB
/
Copy pathThisAddIn.cs
File metadata and controls
206 lines (174 loc) · 6.85 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
namespace OutlookMacroPlugin
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
public void MoveSelectedToTreatedSD()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("1 - Treated mail", "Servicedesk - Shared Mailbox");
}
public void Sign()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
}
public void ServiceNow()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("1.2 - Sent to ServiceNow", "Servicedesk - Shared Mailbox");
}
public void IBM()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\IBM", "Servicedesk - Shared Mailbox");
}
public void EG()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\EG", "Servicedesk - Shared Mailbox");
}
public void NCR()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\NCR", "Servicedesk - Shared Mailbox");
}
public void Wincor()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\Wincor", "Servicedesk - Shared Mailbox");
}
public void ND()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\NetDesign", "Servicedesk - Shared Mailbox");
}
public void HPE()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("5.1 - HPE Incident Reports", "Servicedesk - Shared Mailbox");
}
public void SIMA()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("5 - SIMA Reports", "Servicedesk - Shared Mailbox");
}
public void MIM()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("8 - MIM", "Servicedesk - Shared Mailbox");
}
public void Lexmark()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\Lexmark", "Servicedesk - Shared Mailbox");
}
public void Atea()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\Atea", "Servicedesk - Shared Mailbox");
}
public void Ricoh()
{
ChangeSelectedSubject("Servicedesk - Shared Mailbox");
MoveSelectedMailsTo("3 - Feedback 3rd. parties\\Ricoh", "Servicedesk - Shared Mailbox");
}
private void ChangeSelectedSubject(string sharedMailbox)
{
Application outlookApp = Application;
NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
Recipient recipient = outlookNamespace.CreateRecipient(sharedMailbox);
recipient.Resolve();
if (recipient.Resolved)
{
var currentUser = outlookNamespace.CurrentUser.Name;
var initials = GetInitials(currentUser);
Explorer explorer = outlookApp.ActiveExplorer();
Selection selection = explorer.Selection;
for (int i = 1; i <= selection.Count; i++)
{
MailItem mailItem = selection[i] as MailItem;
if (mailItem != null)
{
string[] subjectParts = mailItem.Subject.Split(' ');
if (subjectParts[0] != initials)
{
mailItem.Subject = initials + " " + mailItem.Subject;
mailItem.UnRead = false;
mailItem.Save();
}
}
}
}
}
private void MoveSelectedMailsTo(string destinationFolder, string sharedMailbox)
{
Application outlookApp = Application;
NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
Recipient recipient = outlookNamespace.CreateRecipient(sharedMailbox);
recipient.Resolve();
if (recipient.Resolved)
{
Explorer explorer = outlookApp.ActiveExplorer();
Selection selection = explorer.Selection;
MAPIFolder destination = GetFolder(sharedMailbox + "\\" + destinationFolder);
for (int i = 1; i <= selection.Count; i++)
{
MailItem mailItem = selection[i] as MailItem;
if (mailItem != null && destination != null)
{
mailItem.Move(destination);
}
}
}
}
private MAPIFolder GetFolder(string folderPath)
{
Application outlookApp = Application;
NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
string[] folderNames = folderPath.Split(new string[] { "\\" }, StringSplitOptions.None);
MAPIFolder folder = outlookNamespace.Folders[folderNames[0]] as MAPIFolder;
if (folder != null)
{
for (int i = 1; i < folderNames.Length; i++)
{
Folders subFolders = folder.Folders;
folder = subFolders[folderNames[i]] as MAPIFolder;
if (folder == null) break;
}
}
return folder;
}
private string GetInitials(string fullName)
{
var nameParts = fullName.Split(' ');
string initials = string.Empty;
if (nameParts.Length > 1)
{
initials = nameParts[0].Substring(0, 2) + nameParts[1].Substring(0, 1);
}
else
{
initials = nameParts[0].Substring(0, 3);
}
return initials.ToUpper();
}
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
}
}