From d27df3027b9e3ca3bcdbe701d9b98b7a5985c3d0 Mon Sep 17 00:00:00 2001 From: RKrom Date: Mon, 17 Mar 2014 14:54:27 +0100 Subject: [PATCH] Office code cleanup, this could improve the COM-object usage a bit. --- .../Destinations/OutlookDestination.cs | 4 +- .../OfficeExport/OutlookEmailExporter.cs | 248 +++++++++--------- .../OfficeExport/PowerpointExporter.cs | 184 ++++++++----- GreenshotPlugin/Core/ImageOutput.cs | 2 +- GreenshotPlugin/Interop/Base.cs | 2 +- 5 files changed, 252 insertions(+), 188 deletions(-) diff --git a/GreenshotOfficePlugin/Destinations/OutlookDestination.cs b/GreenshotOfficePlugin/Destinations/OutlookDestination.cs index a51e71bbc..e129b2e96 100644 --- a/GreenshotOfficePlugin/Destinations/OutlookDestination.cs +++ b/GreenshotOfficePlugin/Destinations/OutlookDestination.cs @@ -133,7 +133,7 @@ namespace GreenshotOfficePlugin { } public override IEnumerable DynamicDestinations() { - Dictionary inspectorCaptions = OutlookEmailExporter.RetrievePossibleTargets(conf.OutlookAllowExportInMeetings); + Dictionary inspectorCaptions = OutlookEmailExporter.RetrievePossibleTargets(); if (inspectorCaptions != null) { foreach (string inspectorCaption in inspectorCaptions.Keys) { yield return new OutlookDestination(inspectorCaption, inspectorCaptions[inspectorCaption]); @@ -175,7 +175,7 @@ namespace GreenshotOfficePlugin { exportInformation.ExportMade = true; } else { if (!manuallyInitiated) { - Dictionary inspectorCaptions = OutlookEmailExporter.RetrievePossibleTargets(conf.OutlookAllowExportInMeetings); + Dictionary inspectorCaptions = OutlookEmailExporter.RetrievePossibleTargets(); if (inspectorCaptions != null && inspectorCaptions.Count > 0) { List destinations = new List(); destinations.Add(new OutlookDestination()); diff --git a/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs b/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs index af0e5e163..0ff770ca4 100644 --- a/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs @@ -31,6 +31,8 @@ using System.Threading; using System.Runtime.InteropServices; using System.Windows.Forms; using GreenshotPlugin.Core; +using GreenshotOfficePlugin; +using Greenshot.IniFile; namespace Greenshot.Interop.Office { /// @@ -38,6 +40,7 @@ namespace Greenshot.Interop.Office { /// public class OutlookEmailExporter { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(OutlookEmailExporter)); + private static readonly OfficeConfiguration conf = IniConfig.GetIniSection(); private static readonly string SIGNATURE_PATH = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Signatures"); private static Version outlookVersion = null; private static string currentUser = null; @@ -56,9 +59,8 @@ namespace Greenshot.Interop.Office { /// /// A method to retrieve all inspectors which can act as an export target /// - /// bool true if also exporting to meetings /// List with inspector captions (window title) - public static Dictionary RetrievePossibleTargets(bool allowMeetingAsTarget) { + public static Dictionary RetrievePossibleTargets() { Dictionary inspectorCaptions = new Dictionary(); try { using (IOutlookApplication outlookApplication = GetOutlookApplication()) { @@ -71,7 +73,7 @@ namespace Greenshot.Interop.Office { using (var activeExplorer = outlookApplication.ActiveExplorer()) { if (activeExplorer != null) { using (var inlineResponse = activeExplorer.ActiveInlineResponse) { - if (canExportToInspector(inlineResponse, allowMeetingAsTarget)) { + if (canExportToInspector(inlineResponse)) { OlObjectClass currentItemClass = inlineResponse.Class; inspectorCaptions.Add(activeExplorer.Caption, currentItemClass); } @@ -86,7 +88,7 @@ namespace Greenshot.Interop.Office { using (IInspector inspector = outlookApplication.Inspectors[i]) { string inspectorCaption = inspector.Caption; using (IItem currentItem = inspector.CurrentItem) { - if (canExportToInspector(currentItem, allowMeetingAsTarget)) { + if (canExportToInspector(currentItem)) { OlObjectClass currentItemClass = currentItem.Class; inspectorCaptions.Add(inspector.Caption, currentItemClass); } @@ -106,9 +108,8 @@ namespace Greenshot.Interop.Office { /// Return true if we can export to the supplied inspector /// /// the Item to check - /// bool true if also exporting to meetings /// - private static bool canExportToInspector(IItem currentItem, bool allowMeetingAsTarget) { + private static bool canExportToInspector(IItem currentItem) { try { if (currentItem != null) { OlObjectClass currentItemClass = currentItem.Class; @@ -119,7 +120,7 @@ namespace Greenshot.Interop.Office { if (!mailItem.Sent) { return true; } - } else if (outlookVersion.Major >= OUTLOOK_2010 && allowMeetingAsTarget && OlObjectClass.olAppointment.Equals(currentItemClass)) { + } else if (outlookVersion.Major >= OUTLOOK_2010 && conf.OutlookAllowExportInMeetings && OlObjectClass.olAppointment.Equals(currentItemClass)) { //AppointmentItem appointmentItem = COMWrapper.Cast(currentItem); AppointmentItem appointmentItem = (AppointmentItem)currentItem; if (string.IsNullOrEmpty(appointmentItem.Organizer) || (currentUser != null && currentUser.Equals(appointmentItem.Organizer))) { @@ -144,47 +145,48 @@ namespace Greenshot.Interop.Office { /// name of the attachment (used as the tooltip of the image) /// true if it worked public static bool ExportToInspector(string inspectorCaption, string tmpFile, string attachmentName) { - // Assume true, although this might cause issues. - bool allowMeetingAsTarget = true; using (IOutlookApplication outlookApplication = GetOrCreateOutlookApplication()) { - if (outlookApplication != null) { - - if (outlookVersion.Major >= OUTLOOK_2013) { - // Check inline "panel" for Outlook 2013 - using (var activeExplorer = outlookApplication.ActiveExplorer()) { - if (activeExplorer != null) { - var currentCaption = activeExplorer.Caption; - if (currentCaption.StartsWith(inspectorCaption)) { - using (var inlineResponse = activeExplorer.ActiveInlineResponse) { - using (IItem currentItem = activeExplorer.ActiveInlineResponse) { - if (canExportToInspector(inlineResponse, allowMeetingAsTarget)) { - try { - return ExportToInspector(activeExplorer, currentItem, tmpFile, attachmentName); - } catch (Exception exExport) { - LOG.Error("Export to " + currentCaption + " failed.", exExport); - } - } + if (outlookApplication == null) { + return false; + } + if (outlookVersion.Major >= OUTLOOK_2013) { + // Check inline "panel" for Outlook 2013 + using (var activeExplorer = outlookApplication.ActiveExplorer()) { + if (activeExplorer == null) { + return false; + } + var currentCaption = activeExplorer.Caption; + if (currentCaption.StartsWith(inspectorCaption)) { + using (var inlineResponse = activeExplorer.ActiveInlineResponse) { + using (IItem currentItem = activeExplorer.ActiveInlineResponse) { + if (canExportToInspector(inlineResponse)) { + try { + return ExportToInspector(activeExplorer, currentItem, tmpFile, attachmentName); + } catch (Exception exExport) { + LOG.Error("Export to " + currentCaption + " failed.", exExport); } } } } } } + } - IInspectors inspectors = outlookApplication.Inspectors; - if (inspectors != null && inspectors.Count > 0) { - LOG.DebugFormat("Got {0} inspectors to check", inspectors.Count); - for (int i = 1; i <= inspectors.Count; i++) { - using (IInspector inspector = outlookApplication.Inspectors[i]) { - string currentCaption = inspector.Caption; - if (currentCaption.StartsWith(inspectorCaption)) { - using (IItem currentItem = inspector.CurrentItem) { - if (canExportToInspector(currentItem, allowMeetingAsTarget)) { - try { - return ExportToInspector(inspector, currentItem, tmpFile, attachmentName); - } catch (Exception exExport) { - LOG.Error("Export to " + currentCaption + " failed.", exExport); - } + using (IInspectors inspectors = outlookApplication.Inspectors) { + if (inspectors == null || inspectors.Count == 0) { + return false; + } + LOG.DebugFormat("Got {0} inspectors to check", inspectors.Count); + for (int i = 1; i <= inspectors.Count; i++) { + using (IInspector inspector = outlookApplication.Inspectors[i]) { + string currentCaption = inspector.Caption; + if (currentCaption.StartsWith(inspectorCaption)) { + using (IItem currentItem = inspector.CurrentItem) { + if (canExportToInspector(currentItem)) { + try { + return ExportToInspector(inspector, currentItem, tmpFile, attachmentName); + } catch (Exception exExport) { + LOG.Error("Export to " + currentCaption + " failed.", exExport); } } } @@ -252,12 +254,7 @@ namespace Greenshot.Interop.Office { try { if (WordExporter.InsertIntoExistingDocument(wordDocument.Application, wordDocument, tmpFile, null, null)) { LOG.Info("Inserted into Wordmail"); - - // check the format afterwards, otherwise we lose the selection - //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) { - // LOG.Info("Changing format to HTML."); - // currentMail.BodyFormat = OlBodyFormat.olFormatHTML; - //} + wordDocument.Dispose(); return true; } } catch (Exception exportException) { @@ -324,6 +321,7 @@ namespace Greenshot.Interop.Office { IPropertyAccessor propertyAccessor = attachment.PropertyAccessor; propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID); } catch { + // Ignore } } } @@ -347,90 +345,98 @@ namespace Greenshot.Interop.Office { /// /// private static void ExportToNewEmail(IOutlookApplication outlookApplication, EmailFormat format, string tmpFile, string subject, string attachmentName, string to, string CC, string BCC, string url) { - IItem newItem = outlookApplication.CreateItem(OlItemType.olMailItem); - if (newItem == null) { - return; - } - //MailItem newMail = COMWrapper.Cast(newItem); - MailItem newMail = (MailItem)newItem; - newMail.Subject = subject; - if (!string.IsNullOrEmpty(to)) { - newMail.To = to; - } - if (!string.IsNullOrEmpty(CC)) { - newMail.CC = CC; - } - if (!string.IsNullOrEmpty(BCC)) { - newMail.BCC = BCC; - } - newMail.BodyFormat = OlBodyFormat.olFormatHTML; - string bodyString = null; - // Read the default signature, if nothing found use empty email - try { - bodyString = GetOutlookSignature(format); - } catch (Exception e) { - LOG.Error("Problem reading signature!", e); - } - switch (format) { - case EmailFormat.Text: - newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 1, attachmentName); - newMail.BodyFormat = OlBodyFormat.olFormatPlain; - if (bodyString == null) { - bodyString = ""; - } - newMail.Body = bodyString; - break; - case EmailFormat.HTML: - default: - string contentID = Path.GetFileName(tmpFile); - // Create the attachment - using (IAttachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) { - // add content ID to the attachment - if (outlookVersion.Major >= OUTLOOK_2007) { - try { - contentID = Guid.NewGuid().ToString(); - IPropertyAccessor 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); + using (IItem newItem = outlookApplication.CreateItem(OlItemType.olMailItem)) { + if (newItem == null) { + return; + } + //MailItem newMail = COMWrapper.Cast(newItem); + MailItem newMail = (MailItem)newItem; + newMail.Subject = subject; + if (!string.IsNullOrEmpty(to)) { + newMail.To = to; + } + if (!string.IsNullOrEmpty(CC)) { + newMail.CC = CC; + } + if (!string.IsNullOrEmpty(BCC)) { + newMail.BCC = BCC; + } + newMail.BodyFormat = OlBodyFormat.olFormatHTML; + string bodyString = null; + // Read the default signature, if nothing found use empty email + try { + bodyString = GetOutlookSignature(format); + } catch (Exception e) { + LOG.Error("Problem reading signature!", e); + } + switch (format) { + case EmailFormat.Text: + // Create the attachment (and dispose the COM object after using) + using (IAttachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 1, attachmentName)) { + newMail.BodyFormat = OlBodyFormat.olFormatPlain; + if (bodyString == null) { + bodyString = ""; + } + newMail.Body = bodyString; + } + break; + case EmailFormat.HTML: + default: + string contentID = Path.GetFileName(tmpFile); + // Create the attachment (and dispose the COM object after using) + using (IAttachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) { + // add content ID to the attachment + if (outlookVersion.Major >= OUTLOOK_2007) { + try { + contentID = Guid.NewGuid().ToString(); + IPropertyAccessor 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); + } } } - } - newMail.BodyFormat = OlBodyFormat.olFormatHTML; - string href = ""; - string hrefEnd = ""; - if (!string.IsNullOrEmpty(url)) { - href = string.Format("", url); - hrefEnd = ""; - } - string htmlImgEmbedded = string.Format("
{0}\"{1}\"{3}
", href, attachmentName, contentID, hrefEnd); - string fallbackBody = string.Format("{0}", htmlImgEmbedded); - if (bodyString == null) { - bodyString = fallbackBody; - } else { - int bodyIndex = bodyString.IndexOf("= 0) { - bodyIndex = bodyString.IndexOf(">", bodyIndex) + 1; + newMail.BodyFormat = OlBodyFormat.olFormatHTML; + string href = ""; + string hrefEnd = ""; + if (!string.IsNullOrEmpty(url)) { + href = string.Format("", url); + hrefEnd = ""; + } + string htmlImgEmbedded = string.Format("
{0}\"{1}\"{3}
", href, attachmentName, contentID, hrefEnd); + string fallbackBody = string.Format("{0}", htmlImgEmbedded); + if (bodyString == null) { + bodyString = fallbackBody; + } else { + int bodyIndex = bodyString.IndexOf("= 0) { - bodyString = bodyString.Insert(bodyIndex, htmlImgEmbedded); + bodyIndex = bodyString.IndexOf(">", bodyIndex) + 1; + if (bodyIndex >= 0) { + bodyString = bodyString.Insert(bodyIndex, htmlImgEmbedded); + } else { + bodyString = fallbackBody; + } } else { bodyString = fallbackBody; } - } else { - bodyString = fallbackBody; + } + newMail.HTMLBody = bodyString; + break; + } + // So not save, otherwise the email is always stored in Draft folder.. (newMail.Save();) + newMail.Display(false); + + using (IInspector inspector = newMail.GetInspector()) { + if (inspector != null) { + try { + inspector.Activate(); + } catch { + // Ignore } } - newMail.HTMLBody = bodyString; - break; - } - // So not save, otherwise the email is always stored in Draft folder.. (newMail.Save();) - newMail.Display(false); - newMail.GetInspector().Activate(); - - if (newItem != null) { - newItem.Dispose(); + } } } diff --git a/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs b/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs index 7e8beb6fa..14e82528a 100644 --- a/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs @@ -40,24 +40,32 @@ namespace Greenshot.Interop.Office { /// Get the captions of all the open powerpoint presentations /// /// - public static System.Collections.Generic.List GetPowerpointPresentations() { - System.Collections.Generic.List presentations = new System.Collections.Generic.List(); + public static List GetPowerpointPresentations() { + List foundPresentations = new System.Collections.Generic.List(); try { using (IPowerpointApplication powerpointApplication = COMWrapper.GetInstance()) { - if (powerpointApplication != null) { - if (version == null) { - version = powerpointApplication.Version; - } - LOG.DebugFormat("Open Presentations: {0}", powerpointApplication.Presentations.Count); - for (int i = 1; i <= powerpointApplication.Presentations.Count; i++) { - IPresentation presentation = powerpointApplication.Presentations.item(i); - if (presentation != null && presentation.ReadOnly != MsoTriState.msoTrue) { + if (powerpointApplication == null) { + return foundPresentations; + } + if (version == null) { + version = powerpointApplication.Version; + } + using (IPresentations presentations = powerpointApplication.Presentations) { + LOG.DebugFormat("Open Presentations: {0}", presentations.Count); + for (int i = 1; i <= presentations.Count; i++) { + using (IPresentation presentation = presentations.item(i)) { + if (presentation == null) { + continue; + } + if (presentation.ReadOnly == MsoTriState.msoTrue) { + continue; + } if (isAfter2003()) { if (presentation.Final) { continue; } } - presentations.Add(presentation.Name); + foundPresentations.Add(presentation.Name); } } } @@ -66,7 +74,7 @@ namespace Greenshot.Interop.Office { LOG.Warn("Problem retrieving word destinations, ignoring: ", ex); } - return presentations; + return foundPresentations; } /// @@ -79,11 +87,19 @@ namespace Greenshot.Interop.Office { /// public static bool ExportToPresentation(string presentationName, string tmpFile, Size imageSize, string title) { using (IPowerpointApplication powerpointApplication = COMWrapper.GetInstance()) { - if (powerpointApplication != null) { - LOG.DebugFormat("Open Presentations: {0}", powerpointApplication.Presentations.Count); - for (int i = 1; i <= powerpointApplication.Presentations.Count; i++) { - IPresentation presentation = powerpointApplication.Presentations.item(i); - if (presentation != null && presentation.Name.StartsWith(presentationName)) { + if (powerpointApplication == null) { + return false; + } + using (IPresentations presentations = powerpointApplication.Presentations) { + LOG.DebugFormat("Open Presentations: {0}", presentations.Count); + for (int i = 1; i <= presentations.Count; i++) { + using (IPresentation presentation = presentations.item(i)) { + if (presentation == null) { + continue; + } + if (!presentation.Name.StartsWith(presentationName)) { + continue; + } try { AddPictureToPresentation(presentation, tmpFile, imageSize, title); return true; @@ -97,47 +113,62 @@ namespace Greenshot.Interop.Office { return false; } + /// + /// Internal method to add a picture to a presentation + /// + /// + /// + /// + /// private static void AddPictureToPresentation(IPresentation presentation, string tmpFile, Size imageSize, string title) { - if (presentation != null) { - //ISlide slide = presentation.Slides.AddSlide( presentation.Slides.Count + 1, PPSlideLayout.ppLayoutPictureWithCaption); - ISlide slide; - float left = (presentation.PageSetup.SlideWidth / 2) - (imageSize.Width / 2) ; - float top = (presentation.PageSetup.SlideHeight / 2) - (imageSize.Height / 2); - float width = imageSize.Width; - float height = imageSize.Height; - bool isLayoutPictureWithCaption = false; - IShape shapeForCaption = null; - bool hasScaledWidth = false; - bool hasScaledHeight = false; + if (presentation == null) { + return; + } + ISlide slide; + float left = (presentation.PageSetup.SlideWidth / 2) - (imageSize.Width / 2) ; + float top = (presentation.PageSetup.SlideHeight / 2) - (imageSize.Height / 2); + float width = imageSize.Width; + float height = imageSize.Height; + bool isLayoutPictureWithCaption = false; + IShape shapeForCaption = null; + bool hasScaledWidth = false; + bool hasScaledHeight = false; + + // Try to create the slide + using (ISlides slides = presentation.Slides) { try { - slide = presentation.Slides.Add(presentation.Slides.Count + 1, (int)PPSlideLayout.ppLayoutPictureWithCaption); + slide = slides.Add(slides.Count + 1, (int)PPSlideLayout.ppLayoutPictureWithCaption); isLayoutPictureWithCaption = true; // Shapes[2] is the image shape on this layout. shapeForCaption = slide.Shapes.item(1); - IShape shapeForLocation = slide.Shapes.item(2); + using (IShape shapeForLocation = slide.Shapes.item(2)) { + if (width > shapeForLocation.Width) { + width = shapeForLocation.Width; + left = shapeForLocation.Left; + hasScaledWidth = true; + } else { + shapeForLocation.Left = left; + } + shapeForLocation.Width = imageSize.Width; - if (width > shapeForLocation.Width) { - width = shapeForLocation.Width; - left = shapeForLocation.Left; - hasScaledWidth = true; - } else { - shapeForLocation.Left = left; + if (height > shapeForLocation.Height) { + height = shapeForLocation.Height; + top = shapeForLocation.Top; + hasScaledHeight = true; + } else { + top = (shapeForLocation.Top + (shapeForLocation.Height / 2)) - (imageSize.Height / 2); + } + shapeForLocation.Height = imageSize.Height; } - shapeForLocation.Width = imageSize.Width; - - if (height > shapeForLocation.Height) { - height = shapeForLocation.Height; - top = shapeForLocation.Top; - hasScaledHeight = true; - } else { - top = (shapeForLocation.Top + (shapeForLocation.Height / 2)) - (imageSize.Height / 2); - } - shapeForLocation.Height = imageSize.Height; } catch (Exception e) { LOG.Error(e); - slide = presentation.Slides.Add(presentation.Slides.Count + 1, (int)PPSlideLayout.ppLayoutBlank); + // didn't work. Use simple slide layout + slide = slides.Add(slides.Count + 1, (int)PPSlideLayout.ppLayoutBlank); } - IShape shape = slide.Shapes.AddPicture(tmpFile, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, width, height); + } + + // Make sure the picture is added and correctly scaled + using (IShape shape = slide.Shapes.AddPicture(tmpFile, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, width, height)) { shape.LockAspectRatio = MsoTriState.msoTrue; shape.ScaleHeight(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromMiddle); shape.ScaleWidth(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromMiddle); @@ -145,37 +176,64 @@ namespace Greenshot.Interop.Office { shape.Width = width; } if (hasScaledHeight) { - shape.Height = height; + shape.Height = height; } shape.Left = left; shape.Top = top; shape.AlternativeText = title; - if (isLayoutPictureWithCaption && shapeForCaption != null) { + } + + // Try settings the caption + if (shapeForCaption != null) { + if (isLayoutPictureWithCaption) { try { // Using try/catch to make sure problems with the text range don't give an exception. - ITextFrame textFrame = shapeForCaption.TextFrame; - textFrame.TextRange.Text = title; + using (ITextFrame textFrame = shapeForCaption.TextFrame) { + textFrame.TextRange.Text = title; + } } catch (Exception ex) { LOG.Warn("Problem setting the title to a text-range", ex); } } - presentation.Application.ActiveWindow.View.GotoSlide(slide.SlideNumber); - presentation.Application.Activate(); + shapeForCaption.Dispose(); + } + + // Show the window, and show the new slide + using (IPowerpointApplication application = presentation.Application) { + using (IPowerpointWindow activeWindow = application.ActiveWindow) { + using (IPowerpointView view = activeWindow.View) { + view.GotoSlide(slide.SlideNumber); + } + } + application.Activate(); + } + if (slide != null) { + slide.Dispose(); } } + /// + /// Insert a capture into a new presentation + /// + /// + /// + /// + /// public static bool InsertIntoNewPresentation(string tmpFile, Size imageSize, string title) { bool isPictureAdded = false; using (IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance()) { - if (powerpointApplication != null) { - powerpointApplication.Visible = true; - IPresentation presentation = powerpointApplication.Presentations.Add(MsoTriState.msoTrue); - try { - AddPictureToPresentation(presentation, tmpFile, imageSize, title); - isPictureAdded = true; - presentation.Application.Activate(); - } catch (Exception e) { - LOG.Error(e); + if (powerpointApplication == null) { + return isPictureAdded; + } + powerpointApplication.Visible = true; + using (IPresentations presentations = powerpointApplication.Presentations) { + using (IPresentation presentation = presentations.Add(MsoTriState.msoTrue)) { + try { + AddPictureToPresentation(presentation, tmpFile, imageSize, title); + isPictureAdded = true; + } catch (Exception e) { + LOG.Error(e); + } } } } diff --git a/GreenshotPlugin/Core/ImageOutput.cs b/GreenshotPlugin/Core/ImageOutput.cs index ffda8f488..b9cbbe8fa 100644 --- a/GreenshotPlugin/Core/ImageOutput.cs +++ b/GreenshotPlugin/Core/ImageOutput.cs @@ -555,7 +555,7 @@ namespace GreenshotPlugin.Core { tmpFileCache.Remove(tmpfile); } return true; - } catch (Exception e) { + } catch (Exception) { } return false; } diff --git a/GreenshotPlugin/Interop/Base.cs b/GreenshotPlugin/Interop/Base.cs index b985c83b6..af344c6c2 100644 --- a/GreenshotPlugin/Interop/Base.cs +++ b/GreenshotPlugin/Interop/Base.cs @@ -6,6 +6,6 @@ namespace Greenshot.Interop { /// /// Common properties that has appreared in almost all objects /// - public interface Common : IDisposable { + public interface ICommon : IDisposable { } }