-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRibbon.cs
183 lines (153 loc) · 6.34 KB
/
Ribbon.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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Office.Core;
// TODO: Follow these steps to enable the Ribbon (XML) item:
// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.
// protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
// {
// return new Ribbon();
// }
// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
// actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
// move your code from the event handlers to the callback methods and modify the code to work with the
// Ribbon extensibility (RibbonX) programming model.
// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.
// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.
namespace MailSorter
{
[ComVisible(true)]
public class Ribbon : IRibbonExtensibility
{
private IRibbonUI _ribbon;
private Microsoft.Office.Interop.Outlook.Folder _destinationFolder = null;
public Ribbon()
{
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
string returnText = string.Empty;
switch (ribbonID)
{
case "Microsoft.Outlook.Explorer":
returnText = GetResourceText("MailSorter.Ribbon.xml");
break;
default:
returnText = string.Empty;
break;
}
return returnText;
}
#endregion
#region Ribbon Callbacks
//Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1
public void Ribbon_Load(Microsoft.Office.Core.IRibbonUI ribbonUI)
{
_ribbon = ribbonUI;
}
public void FileMessage_OnClick(Microsoft.Office.Core.IRibbonControl control)
{
var app = new Microsoft.Office.Interop.Outlook.Application();
var explorer = app.ActiveExplorer();
Microsoft.Office.Interop.Outlook.Selection selections = explorer.Selection;
Microsoft.Office.Interop.Outlook.Folder inbox = app.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox) as Microsoft.Office.Interop.Outlook.Folder;
foreach (var selection in selections)
{
var personName = string.Empty;
if (selection is Microsoft.Office.Interop.Outlook.MailItem)
{
personName = ((Microsoft.Office.Interop.Outlook.MailItem)selection).SenderName;
_destinationFolder = GetMatchingFolder(inbox, personName);
MoveMailItem((Microsoft.Office.Interop.Outlook.MailItem)selection, personName);
}
else if (selection is Microsoft.Office.Interop.Outlook.MeetingItem)
{
personName = ((Microsoft.Office.Interop.Outlook.MeetingItem)selection).SenderName;
_destinationFolder = GetMatchingFolder(inbox, personName);
MoveMeetingItem((Microsoft.Office.Interop.Outlook.MeetingItem)selection, personName);
}
else
{
MessageBox.Show("Unknown message type, can't move it.");
break;
}
_destinationFolder = null;
}
_destinationFolder = null;
}
#endregion
#region Helpers
private void MoveMailItem(Microsoft.Office.Interop.Outlook.MailItem item, string personName)
{
if (_destinationFolder != null)
{
item.UnRead = false;
item.Move(_destinationFolder);
}
else
{
MessageBox.Show("Folder not found:" + personName);
}
}
private void MoveMeetingItem(Microsoft.Office.Interop.Outlook.MeetingItem item, string personName)
{
if (_destinationFolder != null)
{
item.UnRead = false;
item.Move(_destinationFolder);
}
else
{
MessageBox.Show("Folder not found:" + personName);
}
}
private Microsoft.Office.Interop.Outlook.Folder GetMatchingFolder(Microsoft.Office.Interop.Outlook.Folder folder, string personName)
{
if (folder.Name != personName && _destinationFolder == null)
{
foreach (Microsoft.Office.Interop.Outlook.Folder subFolder in folder.Folders)
{
if (subFolder.Name != personName && _destinationFolder == null)
{
_destinationFolder = GetMatchingFolder(subFolder, personName);
}
else
{
if (_destinationFolder == null)
{
_destinationFolder = subFolder;
}
}
}
}
else
{
_destinationFolder = folder;
}
return _destinationFolder;
}
private static string GetResourceText(string resourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] resourceNames = asm.GetManifestResourceNames();
for (int i = 0; i < resourceNames.Length; ++i)
{
if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
{
if (resourceReader != null)
{
return resourceReader.ReadToEnd();
}
}
}
}
return null;
}
#endregion
}
}