From 5076163b92ac6438765fea609d8038708fb82b7a Mon Sep 17 00:00:00 2001 From: RKrom Date: Thu, 17 May 2012 14:53:38 +0000 Subject: [PATCH] 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 --- GreenshotInterop/Interop/COMWrapper.cs | 1 + .../OfficeExport/OutlookEmailExporter.cs | 94 ++++++++++--------- GreenshotInterop/OfficeExport/WordExporter.cs | 19 ++-- 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/GreenshotInterop/Interop/COMWrapper.cs b/GreenshotInterop/Interop/COMWrapper.cs index 0eb435c3c..ade06c33d 100644 --- a/GreenshotInterop/Interop/COMWrapper.cs +++ b/GreenshotInterop/Interop/COMWrapper.cs @@ -249,6 +249,7 @@ namespace Greenshot.Interop { /// private void Dispose(bool disposing) { if (null != this._COMObject) { + LOG.DebugFormat("Disposing {0}", this._InterceptType.ToString()); if (Marshal.IsComObject(this._COMObject)) { try { while (Marshal.ReleaseComObject(this._COMObject) > 0) ; diff --git a/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs b/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs index 7331dc468..056768b75 100644 --- a/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs +++ b/GreenshotInterop/OfficeExport/OutlookEmailExporter.cs @@ -59,14 +59,18 @@ namespace Greenshot.Interop.Office { return null; } - Inspectors inspectors = outlookApplication.Inspectors; - if (inspectors != null && inspectors.Count > 0) { - for (int i = 1; i <= inspectors.Count; i++) { - Inspector inspector = outlookApplication.Inspectors[i]; - if (canExportToInspector(inspector, allowMeetingAsTarget)) { - Item currentItem = inspector.CurrentItem; - OlObjectClass currentItemClass = currentItem.Class; - inspectorCaptions.Add(inspector.Caption, currentItemClass); + using (Inspectors inspectors = outlookApplication.Inspectors) { + if (inspectors != null && inspectors.Count > 0) { + for (int i = 1; i <= inspectors.Count; i++) { + using (Inspector inspector = outlookApplication.Inspectors[i]) { + string inspectorCaption = inspector.Caption; + using (Item currentItem = inspector.CurrentItem) { + if (canExportToInspector(currentItem, allowMeetingAsTarget)) { + OlObjectClass currentItemClass = currentItem.Class; + inspectorCaptions.Add(inspector.Caption, currentItemClass); + } + } + } } } } @@ -80,12 +84,11 @@ namespace Greenshot.Interop.Office { /// /// Return true if we can export to the supplied inspector /// - /// the Inspector to check + /// the Item to check /// bool true if also exporting to meetings /// - private static bool canExportToInspector(Inspector inspector, bool allowMeetingAsTarget) { + private static bool canExportToInspector(Item currentItem, bool allowMeetingAsTarget) { try { - Item currentItem = inspector.CurrentItem; if (currentItem != null) { OlObjectClass currentItemClass = currentItem.Class; if (OlObjectClass.olMail.Equals(currentItemClass)) { @@ -101,7 +104,7 @@ namespace Greenshot.Interop.Office { if (string.IsNullOrEmpty(appointmentItem.Organizer) || (currentUser == null && currentUser.Equals(appointmentItem.Organizer))) { return true; } 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) { LOG.DebugFormat("Got {0} inspectors to check", inspectors.Count); for (int i = 1; i <= inspectors.Count; i++) { - Inspector inspector = outlookApplication.Inspectors[i]; - string currentCaption = inspector.Caption; - if (currentCaption.StartsWith(inspectorCaption)) { - if (canExportToInspector(inspector, allowMeetingAsTarget)) { - try { - return ExportToInspector(inspector, tmpFile, attachmentName); - } catch (Exception exExport) { - LOG.Error("Export to " + currentCaption + " failed.", exExport); + using (Inspector inspector = outlookApplication.Inspectors[i]) { + string currentCaption = inspector.Caption; + if (currentCaption.StartsWith(inspectorCaption)) { + using (Item currentItem = inspector.CurrentItem) { + if (canExportToInspector(currentItem, allowMeetingAsTarget)) { + try { + return ExportToInspector(inspector, currentItem, tmpFile, attachmentName); + } catch (Exception exExport) { + LOG.Error("Export to " + currentCaption + " failed.", exExport); + } + } } } } @@ -149,12 +155,12 @@ namespace Greenshot.Interop.Office { /// /// Export the file to the supplied inspector /// - /// + /// Inspector + /// Item /// /// /// - private static bool ExportToInspector(Inspector inspector, string tmpFile, string attachmentName) { - Item currentItem = inspector.CurrentItem; + private static bool ExportToInspector(Inspector inspector, Item currentItem, string tmpFile, string attachmentName) { if (currentItem == null) { LOG.Warn("No current item."); return false; @@ -250,13 +256,14 @@ namespace Greenshot.Interop.Office { } // Create the attachment (if inlined the attachment isn't visible as attachment!) - Attachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName); - if (outlookVersion.Major >= 12) { - // Add the content id to the attachment, this only works for Outlook >= 2007 - try { - PropertyAccessor propertyAccessor = attachment.PropertyAccessor; - propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); - } catch { + using (Attachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName)) { + if (outlookVersion.Major >= 12) { + // Add the content id to the attachment, this only works for Outlook >= 2007 + try { + PropertyAccessor propertyAccessor = attachment.PropertyAccessor; + propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); + } catch { + } } } } catch (Exception ex) { @@ -299,19 +306,19 @@ namespace Greenshot.Interop.Office { break; case EmailFormat.HTML: 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); - if (outlookVersion.Major >= 12) { - // Add the content id to the attachment - try { - contentID = Guid.NewGuid().ToString(); - PropertyAccessor propertyAccessor = attachment.PropertyAccessor; - propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); - } catch { - LOG.Info("Error working with the PropertyAccessor, using filename as contentid"); - contentID = Path.GetFileName(tmpFile); + // Create the attachment + using (Attachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) { + // add content ID to the attachment + if (outlookVersion.Major >= 12) { + try { + contentID = Guid.NewGuid().ToString(); + PropertyAccessor propertyAccessor = attachment.PropertyAccessor; + propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); + } 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.Start(); } + if (newItem != null) { + newItem.Dispose(); + } } /// diff --git a/GreenshotInterop/OfficeExport/WordExporter.cs b/GreenshotInterop/OfficeExport/WordExporter.cs index dc3f32c45..6775f0a14 100644 --- a/GreenshotInterop/OfficeExport/WordExporter.cs +++ b/GreenshotInterop/OfficeExport/WordExporter.cs @@ -45,9 +45,10 @@ namespace Greenshot.Interop.Office { using (IWordApplication wordApplication = COMWrapper.GetInstance()) { if (wordApplication != null) { for (int i = 1; i <= wordApplication.Documents.Count; i++) { - IWordDocument wordDocument = wordApplication.Documents.item(i); - if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) { - return InsertIntoExistingDocument(wordDocument, tmpFile); + using (IWordDocument wordDocument = wordApplication.Documents.item(i)) { + if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) { + return InsertIntoExistingDocument(wordDocument, tmpFile); + } } } } @@ -67,9 +68,15 @@ namespace Greenshot.Interop.Office { LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.Message); } } - wordDocument.Application.Activate(); - wordDocument.Activate(); - wordDocument.ActiveWindow.Activate(); + try { + wordDocument.Application.Activate(); + } catch {} + try { + wordDocument.Activate(); + } catch {} + try { + wordDocument.ActiveWindow.Activate(); + } catch {} return true; } return false;