Trying out some stability changes, seems to work without side effects.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1867 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-05-17 14:53:38 +00:00
commit 5076163b92
3 changed files with 66 additions and 48 deletions

View file

@ -249,6 +249,7 @@ namespace Greenshot.Interop {
/// </param> /// </param>
private void Dispose(bool disposing) { private void Dispose(bool disposing) {
if (null != this._COMObject) { if (null != this._COMObject) {
LOG.DebugFormat("Disposing {0}", this._InterceptType.ToString());
if (Marshal.IsComObject(this._COMObject)) { if (Marshal.IsComObject(this._COMObject)) {
try { try {
while (Marshal.ReleaseComObject(this._COMObject) > 0) ; while (Marshal.ReleaseComObject(this._COMObject) > 0) ;

View file

@ -59,14 +59,18 @@ namespace Greenshot.Interop.Office {
return null; return null;
} }
Inspectors inspectors = outlookApplication.Inspectors; using (Inspectors inspectors = outlookApplication.Inspectors) {
if (inspectors != null && inspectors.Count > 0) { if (inspectors != null && inspectors.Count > 0) {
for (int i = 1; i <= inspectors.Count; i++) { for (int i = 1; i <= inspectors.Count; i++) {
Inspector inspector = outlookApplication.Inspectors[i]; using (Inspector inspector = outlookApplication.Inspectors[i]) {
if (canExportToInspector(inspector, allowMeetingAsTarget)) { string inspectorCaption = inspector.Caption;
Item currentItem = inspector.CurrentItem; using (Item currentItem = inspector.CurrentItem) {
OlObjectClass currentItemClass = currentItem.Class; if (canExportToInspector(currentItem, allowMeetingAsTarget)) {
inspectorCaptions.Add(inspector.Caption, currentItemClass); OlObjectClass currentItemClass = currentItem.Class;
inspectorCaptions.Add(inspector.Caption, currentItemClass);
}
}
}
} }
} }
} }
@ -80,12 +84,11 @@ namespace Greenshot.Interop.Office {
/// <summary> /// <summary>
/// Return true if we can export to the supplied inspector /// Return true if we can export to the supplied inspector
/// </summary> /// </summary>
/// <param name="inspector">the Inspector to check</param> /// <param name="currentItem">the Item to check</param>
/// <param name="allowMeetingAsTarget">bool true if also exporting to meetings</param> /// <param name="allowMeetingAsTarget">bool true if also exporting to meetings</param>
/// <returns></returns> /// <returns></returns>
private static bool canExportToInspector(Inspector inspector, bool allowMeetingAsTarget) { private static bool canExportToInspector(Item currentItem, bool allowMeetingAsTarget) {
try { try {
Item currentItem = inspector.CurrentItem;
if (currentItem != null) { if (currentItem != null) {
OlObjectClass currentItemClass = currentItem.Class; OlObjectClass currentItemClass = currentItem.Class;
if (OlObjectClass.olMail.Equals(currentItemClass)) { if (OlObjectClass.olMail.Equals(currentItemClass)) {
@ -101,7 +104,7 @@ namespace Greenshot.Interop.Office {
if (string.IsNullOrEmpty(appointmentItem.Organizer) || (currentUser == null && currentUser.Equals(appointmentItem.Organizer))) { if (string.IsNullOrEmpty(appointmentItem.Organizer) || (currentUser == null && currentUser.Equals(appointmentItem.Organizer))) {
return true; return true;
} else { } else {
LOG.DebugFormat("Not exporting to {0}, as organizer is {1} and currentuser {2}", inspector.Caption, appointmentItem.Organizer, currentUser); LOG.DebugFormat("Not exporting, as organizer is {1} and currentuser {2}", appointmentItem.Organizer, currentUser);
} }
} }
} }
@ -128,14 +131,17 @@ namespace Greenshot.Interop.Office {
if (inspectors != null && inspectors.Count > 0) { if (inspectors != null && inspectors.Count > 0) {
LOG.DebugFormat("Got {0} inspectors to check", inspectors.Count); 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]; using (Inspector inspector = outlookApplication.Inspectors[i]) {
string currentCaption = inspector.Caption; string currentCaption = inspector.Caption;
if (currentCaption.StartsWith(inspectorCaption)) { if (currentCaption.StartsWith(inspectorCaption)) {
if (canExportToInspector(inspector, allowMeetingAsTarget)) { using (Item currentItem = inspector.CurrentItem) {
try { if (canExportToInspector(currentItem, allowMeetingAsTarget)) {
return ExportToInspector(inspector, tmpFile, attachmentName); try {
} catch (Exception exExport) { return ExportToInspector(inspector, currentItem, tmpFile, attachmentName);
LOG.Error("Export to " + currentCaption + " failed.", exExport); } catch (Exception exExport) {
LOG.Error("Export to " + currentCaption + " failed.", exExport);
}
}
} }
} }
} }
@ -149,12 +155,12 @@ namespace Greenshot.Interop.Office {
/// <summary> /// <summary>
/// Export the file to the supplied inspector /// Export the file to the supplied inspector
/// </summary> /// </summary>
/// <param name="inspector"></param> /// <param name="inspector">Inspector</param>
/// <param name="currentItem">Item</param>
/// <param name="tmpFile"></param> /// <param name="tmpFile"></param>
/// <param name="attachmentName"></param> /// <param name="attachmentName"></param>
/// <returns></returns> /// <returns></returns>
private static bool ExportToInspector(Inspector inspector, string tmpFile, string attachmentName) { private static bool ExportToInspector(Inspector inspector, Item currentItem, string tmpFile, string attachmentName) {
Item currentItem = inspector.CurrentItem;
if (currentItem == null) { if (currentItem == null) {
LOG.Warn("No current item."); LOG.Warn("No current item.");
return false; return false;
@ -250,13 +256,14 @@ namespace Greenshot.Interop.Office {
} }
// 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 = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName); using (Attachment attachment = mailItem.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 {
PropertyAccessor propertyAccessor = attachment.PropertyAccessor; PropertyAccessor propertyAccessor = attachment.PropertyAccessor;
propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID);
} catch { } catch {
}
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
@ -299,19 +306,19 @@ namespace Greenshot.Interop.Office {
break; break;
case EmailFormat.HTML: case EmailFormat.HTML:
default: default:
// Create the attachment
Attachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName);
// add content ID to the attachment
string contentID = Path.GetFileName(tmpFile); string contentID = Path.GetFileName(tmpFile);
if (outlookVersion.Major >= 12) { // Create the attachment
// Add the content id to the attachment using (Attachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) {
try { // add content ID to the attachment
contentID = Guid.NewGuid().ToString(); if (outlookVersion.Major >= 12) {
PropertyAccessor propertyAccessor = attachment.PropertyAccessor; try {
propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); contentID = Guid.NewGuid().ToString();
} catch { PropertyAccessor propertyAccessor = attachment.PropertyAccessor;
LOG.Info("Error working with the PropertyAccessor, using filename as contentid"); propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID);
contentID = Path.GetFileName(tmpFile); } catch {
LOG.Info("Error working with the PropertyAccessor, using filename as contentid");
contentID = Path.GetFileName(tmpFile);
}
} }
} }
@ -363,6 +370,9 @@ namespace Greenshot.Interop.Office {
retryDisplayEmail.IsBackground = true; retryDisplayEmail.IsBackground = true;
retryDisplayEmail.Start(); retryDisplayEmail.Start();
} }
if (newItem != null) {
newItem.Dispose();
}
} }
/// <summary> /// <summary>

View file

@ -45,9 +45,10 @@ namespace Greenshot.Interop.Office {
using (IWordApplication wordApplication = COMWrapper.GetInstance<IWordApplication>()) { using (IWordApplication wordApplication = COMWrapper.GetInstance<IWordApplication>()) {
if (wordApplication != null) { if (wordApplication != null) {
for (int i = 1; i <= wordApplication.Documents.Count; i++) { for (int i = 1; i <= wordApplication.Documents.Count; i++) {
IWordDocument wordDocument = wordApplication.Documents.item(i); using (IWordDocument wordDocument = wordApplication.Documents.item(i)) {
if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) { if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) {
return InsertIntoExistingDocument(wordDocument, tmpFile); return InsertIntoExistingDocument(wordDocument, tmpFile);
}
} }
} }
} }
@ -67,9 +68,15 @@ namespace Greenshot.Interop.Office {
LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.Message); LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.Message);
} }
} }
wordDocument.Application.Activate(); try {
wordDocument.Activate(); wordDocument.Application.Activate();
wordDocument.ActiveWindow.Activate(); } catch {}
try {
wordDocument.Activate();
} catch {}
try {
wordDocument.ActiveWindow.Activate();
} catch {}
return true; return true;
} }
return false; return false;