-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRibbonCopyAllAttachments.cs
93 lines (85 loc) · 3.86 KB
/
RibbonCopyAllAttachments.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
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Application = System.Windows.Forms.Application;
using Exception = System.Exception;
namespace OutlookCopyAttachmentsToClipboard
{
public partial class RibbonCopyAllAttachments
{
private void RibbonCopyAllAttachments_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
try
{
Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
if (selection != null && selection.Count > 0)
{
foreach (Microsoft.Office.Interop.Outlook.MailItem mailItem in selection)
{
if (mailItem.Attachments.Count > 0)
{
System.Collections.Specialized.StringCollection paths = new System.Collections.Specialized.StringCollection();
for (int i = 1; i <= mailItem.Attachments.Count; i++)
{
Microsoft.Office.Interop.Outlook.Attachment attachment = mailItem.Attachments[i];
// Temporary folder to save attachments
string tempPath = System.IO.Path.GetTempPath() + attachment.FileName;
attachment.SaveAsFile(tempPath);
paths.Add(tempPath);
}
// Copy the files' paths to the clipboard
Clipboard.SetFileDropList(paths);
MessageBox.Show("Selected attachments copied to clipboard.");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
}
private void button2_Click(object sender, RibbonControlEventArgs e)
{
//Microsoft.Office.Interop.Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
string uniqueFolderName = Path.GetRandomFileName();
string saveFolderPath = Path.Combine(Path.GetTempPath(), uniqueFolderName);
// Create the directory
Directory.CreateDirectory(saveFolderPath);
try
{
Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
if (selection != null && selection.Count > 0)
{
foreach (Microsoft.Office.Interop.Outlook.MailItem mailItem in selection)
{
if (mailItem.Attachments.Count > 0)
{
System.Collections.Specialized.StringCollection paths = new System.Collections.Specialized.StringCollection();
for (int i = 1; i <= mailItem.Attachments.Count; i++)
{
Microsoft.Office.Interop.Outlook.Attachment attachment = mailItem.Attachments[i];
// Temporary folder to save attachments
string filePath = Path.Combine(saveFolderPath, attachment.FileName);
attachment.SaveAsFile(filePath);
}
}
}
System.Diagnostics.Process.Start(saveFolderPath);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
}
}
}