From 610760a38630e24d7a95a420207f70d5eff4eac2 Mon Sep 17 00:00:00 2001 From: RKrom Date: Mon, 17 Mar 2014 16:27:21 +0100 Subject: [PATCH] More Office-Code cleanup, should use a bit less resources and maybe be more stable. (needs testing) --- .../OfficeExport/ExcelExporter.cs | 118 +++++++-- .../OfficeExport/OutlookEmailExporter.cs | 22 +- .../OfficeExport/PowerpointExporter.cs | 57 ++++- .../OfficeExport/WordExporter.cs | 223 ++++++++++++------ .../OfficeInterop/ExcelInterop.cs | 48 +++- .../OfficeInterop/OfficeInterop.cs | 14 +- 6 files changed, 345 insertions(+), 137 deletions(-) diff --git a/GreenshotOfficePlugin/OfficeExport/ExcelExporter.cs b/GreenshotOfficePlugin/OfficeExport/ExcelExporter.cs index 0503977fa..88915a9e9 100644 --- a/GreenshotOfficePlugin/OfficeExport/ExcelExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/ExcelExporter.cs @@ -32,15 +32,24 @@ namespace Greenshot.Interop.Office { public class ExcelExporter { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ExcelExporter)); private static readonly OfficeConfiguration officeConfiguration = IniConfig.GetIniSection(); + private static Version excelVersion; + /// + /// Get all currently opened workbooks + /// + /// List with names of the workbooks public static List GetWorkbooks() { List currentWorkbooks = new List(); - using (IExcelApplication excelApplication = COMWrapper.GetInstance()) { - if (excelApplication != null) { - for (int i = 1; i <= excelApplication.Workbooks.Count; i++) { - IWorkbook workbook = excelApplication.Workbooks[i]; - if (workbook != null) { - currentWorkbooks.Add(workbook.Name); + using (IExcelApplication excelApplication = GetExcelApplication()) { + if (excelApplication == null) { + return currentWorkbooks; + } + using (IWorkbooks workbooks = excelApplication.Workbooks) { + for (int i = 1; i <= workbooks.Count; i++) { + using (IWorkbook workbook = workbooks[i]) { + if (workbook != null) { + currentWorkbooks.Add(workbook.Name); + } } } } @@ -54,44 +63,103 @@ namespace Greenshot.Interop.Office { /// /// public static void InsertIntoExistingWorkbook(string workbookName, string tmpFile, Size imageSize) { - using (IExcelApplication excelApplication = COMWrapper.GetInstance()) { - if (excelApplication != null) { - for (int i = 1; i <= excelApplication.Workbooks.Count; i++) { - IWorkbook workbook = excelApplication.Workbooks[i]; - if (workbook != null && workbook.Name.StartsWith(workbookName)) { - InsertIntoExistingWorkbook(workbook, tmpFile, imageSize); + using (IExcelApplication excelApplication = GetExcelApplication()) { + if (excelApplication == null) { + return; + } + using (IWorkbooks workbooks = excelApplication.Workbooks) { + for (int i = 1; i <= workbooks.Count; i++) { + using (IWorkbook workbook = workbooks[i]) { + if (workbook != null && workbook.Name.StartsWith(workbookName)) { + InsertIntoExistingWorkbook(workbook, tmpFile, imageSize); + } } } } } } + /// + /// Insert a file into an already created workbook + /// + /// + /// + /// private static void InsertIntoExistingWorkbook(IWorkbook workbook, string tmpFile, Size imageSize) { IWorksheet workSheet = workbook.ActiveSheet; - if (workSheet != null) { - if (workSheet.Shapes != null) { - IShape shape = workSheet.Shapes.AddPicture(tmpFile, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, imageSize.Width, imageSize.Height); - if (shape != null) { - shape.Top = 40; - shape.Left = 40; - shape.LockAspectRatio = MsoTriState.msoTrue; - shape.ScaleHeight(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft); - shape.ScaleWidth(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft); + if (workSheet == null) { + return; + } + using (IShapes shapes = workSheet.Shapes) { + if (shapes != null) { + using (IShape shape = shapes.AddPicture(tmpFile, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, imageSize.Width, imageSize.Height)) { + if (shape != null) { + shape.Top = 40; + shape.Left = 40; + shape.LockAspectRatio = MsoTriState.msoTrue; + shape.ScaleHeight(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft); + shape.ScaleWidth(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft); + } } } } } + /// + /// Add an image-file to a newly created workbook + /// + /// + /// public static void InsertIntoNewWorkbook(string tmpFile, Size imageSize) { - using (IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance()) { + using (IExcelApplication excelApplication = GetOrCreateExcelApplication()) { if (excelApplication != null) { excelApplication.Visible = true; object template = Missing.Value; - IWorkbook workbook = excelApplication.Workbooks.Add(template); - InsertIntoExistingWorkbook(workbook, tmpFile, imageSize); + using (IWorkbooks workbooks = excelApplication.Workbooks) { + IWorkbook workbook = workbooks.Add(template); + InsertIntoExistingWorkbook(workbook, tmpFile, imageSize); + } } } } - } + + /// + /// Call this to get the running Excel application, returns null if there isn't any. + /// + /// IExcelApplication or null + private static IExcelApplication GetExcelApplication() { + IExcelApplication excelApplication = COMWrapper.GetInstance(); + InitializeVariables(excelApplication); + return excelApplication; + } + + /// + /// Call this to get the running Excel application, or create a new instance + /// + /// IExcelApplication + private static IExcelApplication GetOrCreateExcelApplication() { + IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance(); + InitializeVariables(excelApplication); + return excelApplication; + } + + /// + /// Initialize static outlook variables like version and currentuser + /// + /// + private static void InitializeVariables(IExcelApplication excelApplication) { + if (excelApplication == null || excelVersion != null) { + return; + } + try { + excelVersion = new Version(excelApplication.Version); + LOG.InfoFormat("Using Excel {0}", excelVersion); + } catch (Exception exVersion) { + LOG.Error(exVersion); + LOG.Warn("Assuming Excel version 1997."); + excelVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0); + } + } + } } diff --git a/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs b/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs index 0ff770ca4..ef8fed2e9 100644 --- a/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/OutlookEmailExporter.cs @@ -44,10 +44,6 @@ namespace Greenshot.Interop.Office { 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; - private const int OUTLOOK_2003 = 11; - private const int OUTLOOK_2007 = 12; - private const int OUTLOOK_2010 = 14; - private const int OUTLOOK_2013 = 15; // The signature key can be found at: // HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\\9375CFF0413111d3B88A00104B2A6676\ [New Signature] @@ -68,7 +64,7 @@ namespace Greenshot.Interop.Office { return null; } - if (outlookVersion.Major >= OUTLOOK_2013) { + if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2013) { // Check inline "panel" for Outlook 2013 using (var activeExplorer = outlookApplication.ActiveExplorer()) { if (activeExplorer != null) { @@ -120,7 +116,7 @@ namespace Greenshot.Interop.Office { if (!mailItem.Sent) { return true; } - } else if (outlookVersion.Major >= OUTLOOK_2010 && conf.OutlookAllowExportInMeetings && OlObjectClass.olAppointment.Equals(currentItemClass)) { + } else if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_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))) { @@ -149,7 +145,7 @@ namespace Greenshot.Interop.Office { if (outlookApplication == null) { return false; } - if (outlookVersion.Major >= OUTLOOK_2013) { + if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2013) { // Check inline "panel" for Outlook 2013 using (var activeExplorer = outlookApplication.ActiveExplorer()) { if (activeExplorer == null) { @@ -271,7 +267,7 @@ namespace Greenshot.Interop.Office { LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat); string contentID; - if (outlookVersion.Major >= OUTLOOK_2007) { + if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { contentID = Guid.NewGuid().ToString(); } else { LOG.Info("Older Outlook (<2007) found, using filename as contentid."); @@ -315,7 +311,7 @@ namespace Greenshot.Interop.Office { // Create the attachment (if inlined the attachment isn't visible as attachment!) using (IAttachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName)) { - if (outlookVersion.Major >= OUTLOOK_2007) { + if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { // Add the content id to the attachment, this only works for Outlook >= 2007 try { IPropertyAccessor propertyAccessor = attachment.PropertyAccessor; @@ -386,7 +382,7 @@ namespace Greenshot.Interop.Office { // 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) { + if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { try { contentID = Guid.NewGuid().ToString(); IPropertyAccessor propertyAccessor = attachment.PropertyAccessor; @@ -526,11 +522,11 @@ namespace Greenshot.Interop.Office { LOG.InfoFormat("Using Outlook {0}", outlookVersion); } catch (Exception exVersion) { LOG.Error(exVersion); - LOG.Warn("Assuming outlook version 1."); - outlookVersion = new Version(1, 1, 1, 1); + LOG.Warn("Assuming outlook version 1997."); + outlookVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0); } // Preventing retrieval of currentUser if Outlook is older than 2007 - if (outlookVersion.Major >= OUTLOOK_2007) { + if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) { try { INameSpace mapiNamespace = outlookApplication.GetNameSpace("MAPI"); currentUser = mapiNamespace.CurrentUser.Name; diff --git a/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs b/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs index 14e82528a..d60499b64 100644 --- a/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/PowerpointExporter.cs @@ -28,14 +28,12 @@ using Greenshot.Interop; namespace Greenshot.Interop.Office { public class PowerpointExporter { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PowerpointExporter)); - private static string version = null; + private static Version powerpointVersion; public static bool isAfter2003() { - if (version != null) { - return !version.StartsWith("11"); - } - return false; + return powerpointVersion.Major > (int)OfficeVersion.OFFICE_2003; } + /// /// Get the captions of all the open powerpoint presentations /// @@ -43,13 +41,11 @@ namespace Greenshot.Interop.Office { public static List GetPowerpointPresentations() { List foundPresentations = new System.Collections.Generic.List(); try { - using (IPowerpointApplication powerpointApplication = COMWrapper.GetInstance()) { + using (IPowerpointApplication powerpointApplication = GetPowerpointApplication()) { 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++) { @@ -86,7 +82,7 @@ namespace Greenshot.Interop.Office { /// A string with the image title /// public static bool ExportToPresentation(string presentationName, string tmpFile, Size imageSize, string title) { - using (IPowerpointApplication powerpointApplication = COMWrapper.GetInstance()) { + using (IPowerpointApplication powerpointApplication = GetPowerpointApplication()) { if (powerpointApplication == null) { return false; } @@ -221,7 +217,7 @@ namespace Greenshot.Interop.Office { /// public static bool InsertIntoNewPresentation(string tmpFile, Size imageSize, string title) { bool isPictureAdded = false; - using (IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance()) { + using (IPowerpointApplication powerpointApplication = GetOrCreatePowerpointApplication()) { if (powerpointApplication == null) { return isPictureAdded; } @@ -239,5 +235,44 @@ namespace Greenshot.Interop.Office { } return isPictureAdded; } + + /// + /// Call this to get the running powerpoint application, returns null if there isn't any. + /// + /// IPowerpointApplication or null + private static IPowerpointApplication GetPowerpointApplication() { + IPowerpointApplication powerpointApplication = COMWrapper.GetInstance(); + InitializeVariables(powerpointApplication); + return powerpointApplication; + } + + /// + /// Call this to get the running powerpoint application, or create a new instance + /// + /// IPowerpointApplication + private static IPowerpointApplication GetOrCreatePowerpointApplication() { + IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance(); + InitializeVariables(powerpointApplication); + return powerpointApplication; + } + + /// + /// Initialize static outlook variables like version and currentuser + /// + /// IPowerpointApplication + private static void InitializeVariables(IPowerpointApplication powerpointApplication) { + if (powerpointApplication == null || powerpointVersion != null) { + return; + } + try { + powerpointVersion = new Version(powerpointApplication.Version); + LOG.InfoFormat("Using Powerpoint {0}", powerpointVersion); + } catch (Exception exVersion) { + LOG.Error(exVersion); + LOG.Warn("Assuming Powerpoint version 1997."); + powerpointVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0); + } + } + } } diff --git a/GreenshotOfficePlugin/OfficeExport/WordExporter.cs b/GreenshotOfficePlugin/OfficeExport/WordExporter.cs index 268d037b6..92f6c2435 100644 --- a/GreenshotOfficePlugin/OfficeExport/WordExporter.cs +++ b/GreenshotOfficePlugin/OfficeExport/WordExporter.cs @@ -29,15 +29,17 @@ using Greenshot.IniFile; namespace Greenshot.Interop.Office { public class WordExporter { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(WordExporter)); - private static string version = null; + private static Version wordVersion = null; private static OfficeConfiguration config = IniConfig.GetIniSection(); - public static bool isAfter2003() { - if (version != null) { - return !version.StartsWith("11"); - } - return false; + /// + /// Check if the used version is higher than Office 2003 + /// + /// + private static bool isAfter2003() { + return wordVersion.Major > (int)OfficeVersion.OFFICE_2003; } + /// /// Insert the bitmap stored under the tempfile path into the word document with the supplied caption /// @@ -45,12 +47,17 @@ namespace Greenshot.Interop.Office { /// /// public static bool InsertIntoExistingDocument(string wordCaption, string tmpFile) { - using (IWordApplication wordApplication = COMWrapper.GetInstance()) { - if (wordApplication != null) { - for (int i = 1; i <= wordApplication.Documents.Count; i++) { - using (IWordDocument wordDocument = wordApplication.Documents.item(i)) { - if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) { - return InsertIntoExistingDocument(wordApplication, wordDocument, tmpFile, null, null); + using (IWordApplication wordApplication = GetWordApplication()) { + if (wordApplication == null) { + return false; + } + using (IDocuments documents = wordApplication.Documents) { + for (int i = 1; i <= documents.Count; i++) { + using (IWordDocument wordDocument = documents.item(i)) { + using (IWordWindow activeWindow = wordDocument.ActiveWindow) { + if (activeWindow.Caption.StartsWith(wordCaption)) { + return InsertIntoExistingDocument(wordApplication, wordDocument, tmpFile, null, null); + } } } } @@ -75,23 +82,36 @@ namespace Greenshot.Interop.Office { wordDocument.Activate(); } catch { } - if (wordApplication.Selection != null) { + using (ISelection selection = wordApplication.Selection) { + if (selection == null) { + LOG.InfoFormat("No selection to insert {0} into found.", tmpFile); + return false; + } // Add Picture - using (IInlineShape shape = AddPictureToSelection(wordApplication.Selection, tmpFile)) { + using (IInlineShape shape = AddPictureToSelection(selection, tmpFile)) { if (!string.IsNullOrEmpty(address)) { object screentip = Type.Missing; if (!string.IsNullOrEmpty(tooltip)) { screentip = tooltip; } try { - wordDocument.Hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing); + using (IHyperlinks hyperlinks = wordDocument.Hyperlinks) { + hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing); + } } catch (Exception e) { LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message); } } } try { - wordDocument.ActiveWindow.ActivePane.View.Zoom.Percentage = 100; + using (IWordWindow activeWindow = wordDocument.ActiveWindow) { + activeWindow.Activate(); + using (IPane activePane = activeWindow.ActivePane) { + using (IWordView view = activePane.View) { + view.Zoom.Percentage = 100; + } + } + } } catch (Exception e) { if (e.InnerException != null) { LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.InnerException.Message); @@ -101,64 +121,74 @@ namespace Greenshot.Interop.Office { } try { wordApplication.Activate(); - } catch {} + } catch { + } try { wordDocument.Activate(); - } catch {} - try { - wordDocument.ActiveWindow.Activate(); - } catch {} + } catch { + } return true; } - return false; } + /// + /// Helper method to add the file as image to the selection + /// + /// + /// + /// private static IInlineShape AddPictureToSelection(ISelection selection, string tmpFile) { - IInlineShape shape = selection.InlineShapes.AddPicture(tmpFile, false, true, Type.Missing); - // Lock aspect ratio - if (config.WordLockAspectRatio) { - shape.LockAspectRatio = MsoTriState.msoTrue; + using (IInlineShapes shapes = selection.InlineShapes) { + IInlineShape shape = shapes.AddPicture(tmpFile, false, true, Type.Missing); + // Lock aspect ratio + if (config.WordLockAspectRatio) { + shape.LockAspectRatio = MsoTriState.msoTrue; + } + selection.InsertAfter("\r\n"); + return shape; } - selection.InsertAfter("\r\n"); - return shape; } public static void InsertIntoNewDocument(string tmpFile, string address, string tooltip) { - using (IWordApplication wordApplication = COMWrapper.GetOrCreateInstance()) { - if (wordApplication != null) { - wordApplication.Visible = true; - wordApplication.Activate(); - // Create new Document - object template = string.Empty; - object newTemplate = false; - object documentType = 0; - object documentVisible = true; - IWordDocument wordDocument = wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible); - // Add Picture - using (IInlineShape shape = AddPictureToSelection(wordApplication.Selection, tmpFile)) { - if (!string.IsNullOrEmpty(address)) { - object screentip = Type.Missing; - if (!string.IsNullOrEmpty(tooltip)) { - screentip = tooltip; - } - try { - wordDocument.Hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing); - } catch (Exception e) { - LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message); + using (IWordApplication wordApplication = GetOrCreateWordApplication()) { + if (wordApplication == null) { + return; + } + wordApplication.Visible = true; + wordApplication.Activate(); + // Create new Document + object template = string.Empty; + object newTemplate = false; + object documentType = 0; + object documentVisible = true; + using (IDocuments documents = wordApplication.Documents) { + using (IWordDocument wordDocument = documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible)) { + using (ISelection selection = wordApplication.Selection) { + // Add Picture + using (IInlineShape shape = AddPictureToSelection(selection, tmpFile)) { + if (!string.IsNullOrEmpty(address)) { + object screentip = Type.Missing; + if (!string.IsNullOrEmpty(tooltip)) { + screentip = tooltip; + } + try { + using (IHyperlinks hyperlinks = wordDocument.Hyperlinks) { + hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing); + } + } catch (Exception e) { + LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message); + } + } } } - } - try { - wordApplication.Activate(); - } catch { - } - try { - wordDocument.Activate(); - } catch { - } - try { - wordDocument.ActiveWindow.Activate(); - } catch { + try { + wordDocument.Activate(); + } catch { + } + try { + wordDocument.ActiveWindow.Activate(); + } catch { + } } } } @@ -169,31 +199,72 @@ namespace Greenshot.Interop.Office { /// /// public static List GetWordDocuments() { - List documents = new List(); + List openDocuments = new List(); try { - using (IWordApplication wordApplication = COMWrapper.GetInstance()) { - if (wordApplication != null) { - if (version == null) { - version = wordApplication.Version; - } - for (int i = 1; i <= wordApplication.Documents.Count; i++) { - IWordDocument document = wordApplication.Documents.item(i); - if (document.ReadOnly) { - continue; - } - if (isAfter2003()) { - if (document.Final) { + using (IWordApplication wordApplication = GetWordApplication()) { + if (wordApplication == null) { + return openDocuments; + } + using (IDocuments documents = wordApplication.Documents) { + for (int i = 1; i <= documents.Count; i++) { + using (IWordDocument document = documents.item(i)) { + if (document.ReadOnly) { continue; } + if (isAfter2003()) { + if (document.Final) { + continue; + } + } + using (IWordWindow activeWindow = document.ActiveWindow) { + openDocuments.Add(activeWindow.Caption); + } } - documents.Add(document.ActiveWindow.Caption); } } } } catch (Exception ex) { LOG.Warn("Problem retrieving word destinations, ignoring: ", ex); } - return documents; + return openDocuments; + } + + /// + /// Call this to get the running outlook application, returns null if there isn't any. + /// + /// IWordApplication or null + private static IWordApplication GetWordApplication() { + IWordApplication wordApplication = COMWrapper.GetInstance(); + InitializeVariables(wordApplication); + return wordApplication; + } + + /// + /// Call this to get the running word application, or create a new instance + /// + /// IWordApplication + private static IWordApplication GetOrCreateWordApplication() { + IWordApplication wordApplication = COMWrapper.GetOrCreateInstance(); + InitializeVariables(wordApplication); + return wordApplication; + } + + /// + /// Initialize static outlook variables like version and currentuser + /// + /// + private static void InitializeVariables(IWordApplication wordApplication) { + if (wordApplication == null || wordVersion != null) { + return; + } + try { + wordVersion = new Version(wordApplication.Version); + LOG.InfoFormat("Using Word {0}", wordVersion); + } catch (Exception exVersion) { + LOG.Error(exVersion); + LOG.Warn("Assuming Word version 1997."); + wordVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0); + } } } } diff --git a/GreenshotOfficePlugin/OfficeInterop/ExcelInterop.cs b/GreenshotOfficePlugin/OfficeInterop/ExcelInterop.cs index f2fc13159..253946cab 100644 --- a/GreenshotOfficePlugin/OfficeInterop/ExcelInterop.cs +++ b/GreenshotOfficePlugin/OfficeInterop/ExcelInterop.cs @@ -23,38 +23,64 @@ namespace Greenshot.Interop.Office { // See http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.application.aspx [ComProgId("Excel.Application")] public interface IExcelApplication : ICommon { - IWorkbook ActiveWorkbook { get; } + IWorkbook ActiveWorkbook { + get; + } //ISelection Selection {get;} - IWorkbooks Workbooks { get; } - bool Visible { get; set; } + IWorkbooks Workbooks { + get; + } + bool Visible { + get; + set; + } + string Version { + get; + } } // See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.aspx public interface IWorkbooks : ICommon, ICollection { IWorkbook Add(object template); // Use index + 1!! - IWorkbook this[object Index] { get; } + IWorkbook this[object Index] { + get; + } } // See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbook.aspx public interface IWorkbook : ICommon { - IWorksheet ActiveSheet { get; } - string Name { get; } + IWorksheet ActiveSheet { + get; + } + string Name { + get; + } void Activate(); - IWorksheets Worksheets { get; } + IWorksheets Worksheets { + get; + } } // See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._worksheet_members.aspx public interface IWorksheet : ICommon { - IPictures Pictures { get; } - IShapes Shapes {get; } - string Name { get; } + IPictures Pictures { + get; + } + IShapes Shapes { + get; + } + string Name { + get; + } } // See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.iworksheets_members.aspx public interface IWorksheets : ICommon, ICollection { // Use index + 1!! - IWorksheet this[object Index] { get; } + IWorksheet this[object Index] { + get; + } } public interface IPictures : ICommon, ICollection { diff --git a/GreenshotOfficePlugin/OfficeInterop/OfficeInterop.cs b/GreenshotOfficePlugin/OfficeInterop/OfficeInterop.cs index 69cf4cd20..85e7daa5b 100644 --- a/GreenshotOfficePlugin/OfficeInterop/OfficeInterop.cs +++ b/GreenshotOfficePlugin/OfficeInterop/OfficeInterop.cs @@ -21,11 +21,23 @@ using System.Collections; namespace Greenshot.Interop.Office { + public enum OfficeVersion : int { + OFFICE_97 = 8, + OFFICE_2000 = 9, + OFFICE_2002 = 10, + OFFICE_2003 = 11, + OFFICE_2007 = 12, + OFFICE_2010 = 14, + OFFICE_2013 = 15 + } + /// /// If the "type" this[object index] { get; } is implemented, use index + 1!!! (starts at 1) /// public interface ICollection : ICommon, IEnumerable { - int Count { get; } + int Count { + get; + } void Remove(int index); } }