mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 13:10:00 -07:00
Outlook export should now also work for appointments when using Outlook 2007 and later.
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1665 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
fe9e5e6c35
commit
9bf9c0b8d3
2 changed files with 97 additions and 41 deletions
|
@ -70,8 +70,12 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
DateTime SentOn { get; }
|
DateTime SentOn { get; }
|
||||||
OlBodyFormat BodyFormat { get; set; }
|
OlBodyFormat BodyFormat { get; set; }
|
||||||
PropertyAccessor PropertyAccessor { get; }
|
PropertyAccessor PropertyAccessor { get; }
|
||||||
|
// MailItem
|
||||||
bool Sent { get; }
|
bool Sent { get; }
|
||||||
object MAPIOBJECT { get; }
|
object MAPIOBJECT { get; }
|
||||||
|
// AppointmentItem
|
||||||
|
string Organizer { get; set; }
|
||||||
|
string SendUsingAccount { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Attachments : Collection {
|
public interface Attachments : Collection {
|
||||||
|
|
|
@ -39,7 +39,7 @@ using Microsoft.Win32;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
namespace Greenshot.Helpers.OfficeInterop {
|
namespace Greenshot.Helpers.OfficeInterop {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Wrapper for Outlook.Application
|
/// Wrapper for Outlook.Application, see: http://msdn.microsoft.com/en-us/library/aa210897%28v=office.11%29.aspx
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ComProgId("Outlook.Application")]
|
[ComProgId("Outlook.Application")]
|
||||||
public interface IOutlookApplication : Common {
|
public interface IOutlookApplication : Common {
|
||||||
|
@ -50,6 +50,15 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
object CreateObject(string ObjectName);
|
object CreateObject(string ObjectName);
|
||||||
Inspector ActiveInspector();
|
Inspector ActiveInspector();
|
||||||
Inspectors Inspectors { get; }
|
Inspectors Inspectors { get; }
|
||||||
|
INameSpace GetNameSpace(string type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface INameSpace : Common {
|
||||||
|
IRecipient CurrentUser { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IRecipient : Common {
|
||||||
|
string Name { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.inspector_members.aspx
|
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.inspector_members.aspx
|
||||||
|
@ -88,6 +97,7 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
|
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
|
||||||
private static readonly string SIGNATURE_PATH = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Signatures");
|
private static readonly string SIGNATURE_PATH = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Signatures");
|
||||||
private static Version outlookVersion = new Version(1,1,1,1);
|
private static Version outlookVersion = new Version(1,1,1,1);
|
||||||
|
private static string currentUser = null;
|
||||||
|
|
||||||
// The signature key can be found at:
|
// The signature key can be found at:
|
||||||
// HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\<DefaultProfile>\9375CFF0413111d3B88A00104B2A6676\<xxxx> [New Signature]
|
// HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\<DefaultProfile>\9375CFF0413111d3B88A00104B2A6676\<xxxx> [New Signature]
|
||||||
|
@ -108,20 +118,13 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
if (outlookApplication == null) {
|
if (outlookApplication == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Inspectors inspectors = outlookApplication.Inspectors;
|
Inspectors inspectors = outlookApplication.Inspectors;
|
||||||
if (inspectors != null && inspectors.Count > 0) {
|
if (inspectors != null && inspectors.Count > 0) {
|
||||||
LOG.DebugFormat("Got {0} inspectors to check", inspectors.Count);
|
|
||||||
for(int i=1; i <= inspectors.Count; i++) {
|
for(int i=1; i <= inspectors.Count; i++) {
|
||||||
Inspector inspector = outlookApplication.Inspectors[i];
|
Inspector inspector = outlookApplication.Inspectors[i];
|
||||||
LOG.DebugFormat("Checking inspector '{0}'", inspector.Caption);
|
if (canExportToInspector(inspector)) {
|
||||||
try {
|
inspectorCaptions.Add(inspector.Caption);
|
||||||
Item currentMail = inspector.CurrentItem;
|
|
||||||
if (currentMail != null && OlObjectClass.olMail.Equals(currentMail.Class) ) {
|
|
||||||
if (currentMail != null && !currentMail.Sent) {
|
|
||||||
inspectorCaptions.Add(inspector.Caption);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,7 +133,34 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
|
LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
|
||||||
}
|
}
|
||||||
return inspectorCaptions;
|
return inspectorCaptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return true if we can export to the supplied inspector
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="inspector"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static bool canExportToInspector(Inspector inspector) {
|
||||||
|
try {
|
||||||
|
Item currentItem = inspector.CurrentItem;
|
||||||
|
if (currentItem != null) {
|
||||||
|
OlObjectClass currentItemClass = currentItem.Class;
|
||||||
|
if (OlObjectClass.olMail.Equals(currentItemClass)) {
|
||||||
|
if (!currentItem.Sent) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else if (outlookVersion.Major >=12 && OlObjectClass.olAppointment.Equals(currentItemClass)) {
|
||||||
|
if (currentUser != null && currentUser.Equals(currentItem.Organizer)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
LOG.DebugFormat("Not exporting to {0}, as organizer is {1} and currentuser {2}", inspector.Caption, currentItem.Organizer, currentUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
LOG.WarnFormat("Couldn't process item due to: {0}", ex.Message);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -151,15 +181,12 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
Inspector inspector = outlookApplication.Inspectors[i];
|
Inspector inspector = outlookApplication.Inspectors[i];
|
||||||
string currentCaption = inspector.Caption;
|
string currentCaption = inspector.Caption;
|
||||||
if (currentCaption.StartsWith(inspectorCaption)) {
|
if (currentCaption.StartsWith(inspectorCaption)) {
|
||||||
try {
|
if (canExportToInspector(inspector)) {
|
||||||
Item currentMail = inspector.CurrentItem;
|
try {
|
||||||
if (currentMail != null && OlObjectClass.olMail.Equals(currentMail.Class)) {
|
return ExportToInspector(inspector, tmpFile, attachmentName);
|
||||||
if (currentMail != null && !currentMail.Sent) {
|
} catch (Exception exExport) {
|
||||||
LOG.InfoFormat("Export requested to \"{0}\" exporting to \"{1}\"", inspectorCaption, currentCaption);
|
LOG.Error("Export to " + currentCaption + " failed.", exExport);
|
||||||
return ExportToInspector(inspector, tmpFile, attachmentName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (Exception) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,19 +196,29 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Export the file to the supplied inspector
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="inspector"></param>
|
||||||
|
/// <param name="tmpFile"></param>
|
||||||
|
/// <param name="attachmentName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
private static bool ExportToInspector(Inspector inspector, string tmpFile, string attachmentName) {
|
private static bool ExportToInspector(Inspector inspector, string tmpFile, string attachmentName) {
|
||||||
Item currentMail = inspector.CurrentItem;
|
Item currentItem = inspector.CurrentItem;
|
||||||
if (currentMail == null) {
|
if (currentItem == null) {
|
||||||
LOG.Warn("No current item.");
|
LOG.Warn("No current item.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!OlObjectClass.olMail.Equals(currentMail.Class)) {
|
OlObjectClass itemClass = currentItem.Class;
|
||||||
LOG.Warn("Item is no mail.");
|
bool isMail = OlObjectClass.olMail.Equals(itemClass);
|
||||||
|
bool isAppointment = OlObjectClass.olAppointment.Equals(itemClass);
|
||||||
|
if (!isMail && !isAppointment) {
|
||||||
|
LOG.Warn("Item is no mail or appointment.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (currentMail.Sent) {
|
if (isMail && currentItem.Sent) {
|
||||||
LOG.WarnFormat("Item already sent, can't export to {0}", currentMail.Subject);
|
LOG.WarnFormat("Item already sent, can't export to {0}", currentItem.Subject);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,8 +226,6 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
// This also ensures that the window is visible!
|
// This also ensures that the window is visible!
|
||||||
inspector.Activate();
|
inspector.Activate();
|
||||||
|
|
||||||
LOG.InfoFormat("Email '{0}' has format: {1}", currentMail.Subject, currentMail.BodyFormat);
|
|
||||||
|
|
||||||
// Check for wordmail, if so use the wordexporter
|
// Check for wordmail, if so use the wordexporter
|
||||||
// http://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx
|
// http://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx
|
||||||
// Earlier versions of Outlook also supported an Inspector.HTMLEditor object property, but since Internet Explorer is no longer the rendering engine for HTML messages and posts, HTMLEditor is no longer supported.
|
// Earlier versions of Outlook also supported an Inspector.HTMLEditor object property, but since Internet Explorer is no longer the rendering engine for HTML messages and posts, HTMLEditor is no longer supported.
|
||||||
|
@ -205,9 +240,14 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
//}
|
//}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
} else if (isAppointment) {
|
||||||
|
LOG.Info("Can't export to an appointment if no word editor is used");
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
LOG.Debug("Wordmail editor is not supported");
|
LOG.Info("Trying export for word < 2007.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG.InfoFormat("Item '{0}' has format: {1}", currentItem.Subject, currentItem.BodyFormat);
|
||||||
|
|
||||||
string contentID;
|
string contentID;
|
||||||
if (outlookVersion.Major >=12 ) {
|
if (outlookVersion.Major >=12 ) {
|
||||||
|
@ -224,7 +264,7 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
//}
|
//}
|
||||||
|
|
||||||
bool inlinePossible = false;
|
bool inlinePossible = false;
|
||||||
if (OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
|
if (OlBodyFormat.olFormatHTML.Equals(currentItem.BodyFormat)) {
|
||||||
// if html we can try to inline it
|
// if html we can try to inline it
|
||||||
// The following might cause a security popup... can't ignore it.
|
// The following might cause a security popup... can't ignore it.
|
||||||
try {
|
try {
|
||||||
|
@ -253,7 +293,7 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the attachment (if inlined the attachment isn't visible as attachment!)
|
// Create the attachment (if inlined the attachment isn't visible as attachment!)
|
||||||
Attachment attachment = currentMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible?0:1, attachmentName);
|
Attachment attachment = currentItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible?0:1, attachmentName);
|
||||||
if (outlookVersion.Major >=12) {
|
if (outlookVersion.Major >=12) {
|
||||||
// Add the content id to the attachment, this only works for Outlook >= 2007
|
// Add the content id to the attachment, this only works for Outlook >= 2007
|
||||||
try {
|
try {
|
||||||
|
@ -263,7 +303,7 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LOG.WarnFormat("Problem while trying to add attachment to MailItem '{0}' : {1}", inspector.Caption, ex);
|
LOG.WarnFormat("Problem while trying to add attachment to Item '{0}' : {1}", inspector.Caption, ex);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LOG.Debug("Finished!");
|
LOG.Debug("Finished!");
|
||||||
|
@ -425,18 +465,33 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize static outlook variables like version and currentuser
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="outlookApplication"></param>
|
||||||
|
private static void InitializeVariables(IOutlookApplication outlookApplication) {
|
||||||
|
try {
|
||||||
|
outlookVersion = new Version(outlookApplication.Version);
|
||||||
|
LOG.InfoFormat("Using Outlook {0}", outlookVersion);
|
||||||
|
} catch (Exception exVersion) {
|
||||||
|
LOG.Error(exVersion);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
INameSpace mapiNamespace = outlookApplication.GetNameSpace("MAPI");
|
||||||
|
currentUser = mapiNamespace.CurrentUser.Name;
|
||||||
|
LOG.InfoFormat("Current user: {0}", currentUser);
|
||||||
|
} catch (Exception exNS) {
|
||||||
|
LOG.Error(exNS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Call this to get the running outlook application, returns null if there isn't any.
|
/// Call this to get the running outlook application, returns null if there isn't any.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>IOutlookApplication or null</returns>
|
/// <returns>IOutlookApplication or null</returns>
|
||||||
private static IOutlookApplication GetOutlookApplication() {
|
private static IOutlookApplication GetOutlookApplication() {
|
||||||
IOutlookApplication outlookApplication = (IOutlookApplication)COMWrapper.GetInstance(typeof(IOutlookApplication));
|
IOutlookApplication outlookApplication = (IOutlookApplication)COMWrapper.GetInstance(typeof(IOutlookApplication));
|
||||||
try {
|
InitializeVariables(outlookApplication);
|
||||||
if (outlookApplication != null) {
|
|
||||||
outlookVersion = new Version(outlookApplication.Version);
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
}
|
|
||||||
return outlookApplication;
|
return outlookApplication;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -446,10 +501,7 @@ namespace Greenshot.Helpers.OfficeInterop {
|
||||||
/// <returns>IOutlookApplication</returns>
|
/// <returns>IOutlookApplication</returns>
|
||||||
private static IOutlookApplication GetOrCreateOutlookApplication() {
|
private static IOutlookApplication GetOrCreateOutlookApplication() {
|
||||||
IOutlookApplication outlookApplication = (IOutlookApplication)COMWrapper.GetOrCreateInstance(typeof(IOutlookApplication));
|
IOutlookApplication outlookApplication = (IOutlookApplication)COMWrapper.GetOrCreateInstance(typeof(IOutlookApplication));
|
||||||
try {
|
InitializeVariables(outlookApplication);
|
||||||
outlookVersion = new Version(outlookApplication.Version);
|
|
||||||
} catch {
|
|
||||||
}
|
|
||||||
return outlookApplication;
|
return outlookApplication;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue