mirror of
https://github.com/greenshot/greenshot
synced 2025-08-21 05:53:27 -07:00
More Office-Code cleanup, should use a bit less resources and maybe be more stable. (needs testing)
This commit is contained in:
parent
d27df3027b
commit
610760a386
6 changed files with 345 additions and 137 deletions
|
@ -32,15 +32,24 @@ namespace Greenshot.Interop.Office {
|
||||||
public class ExcelExporter {
|
public class ExcelExporter {
|
||||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ExcelExporter));
|
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ExcelExporter));
|
||||||
private static readonly OfficeConfiguration officeConfiguration = IniConfig.GetIniSection<OfficeConfiguration>();
|
private static readonly OfficeConfiguration officeConfiguration = IniConfig.GetIniSection<OfficeConfiguration>();
|
||||||
|
private static Version excelVersion;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get all currently opened workbooks
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>List<string> with names of the workbooks</returns>
|
||||||
public static List<string> GetWorkbooks() {
|
public static List<string> GetWorkbooks() {
|
||||||
List<string> currentWorkbooks = new List<string>();
|
List<string> currentWorkbooks = new List<string>();
|
||||||
using (IExcelApplication excelApplication = COMWrapper.GetInstance<IExcelApplication>()) {
|
using (IExcelApplication excelApplication = GetExcelApplication()) {
|
||||||
if (excelApplication != null) {
|
if (excelApplication == null) {
|
||||||
for (int i = 1; i <= excelApplication.Workbooks.Count; i++) {
|
return currentWorkbooks;
|
||||||
IWorkbook workbook = excelApplication.Workbooks[i];
|
}
|
||||||
if (workbook != null) {
|
using (IWorkbooks workbooks = excelApplication.Workbooks) {
|
||||||
currentWorkbooks.Add(workbook.Name);
|
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 {
|
||||||
/// <param name="workbookName"></param>
|
/// <param name="workbookName"></param>
|
||||||
/// <param name="tmpFile"></param>
|
/// <param name="tmpFile"></param>
|
||||||
public static void InsertIntoExistingWorkbook(string workbookName, string tmpFile, Size imageSize) {
|
public static void InsertIntoExistingWorkbook(string workbookName, string tmpFile, Size imageSize) {
|
||||||
using (IExcelApplication excelApplication = COMWrapper.GetInstance<IExcelApplication>()) {
|
using (IExcelApplication excelApplication = GetExcelApplication()) {
|
||||||
if (excelApplication != null) {
|
if (excelApplication == null) {
|
||||||
for (int i = 1; i <= excelApplication.Workbooks.Count; i++) {
|
return;
|
||||||
IWorkbook workbook = excelApplication.Workbooks[i];
|
}
|
||||||
if (workbook != null && workbook.Name.StartsWith(workbookName)) {
|
using (IWorkbooks workbooks = excelApplication.Workbooks) {
|
||||||
InsertIntoExistingWorkbook(workbook, tmpFile, imageSize);
|
for (int i = 1; i <= workbooks.Count; i++) {
|
||||||
|
using (IWorkbook workbook = workbooks[i]) {
|
||||||
|
if (workbook != null && workbook.Name.StartsWith(workbookName)) {
|
||||||
|
InsertIntoExistingWorkbook(workbook, tmpFile, imageSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Insert a file into an already created workbook
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="workbook"></param>
|
||||||
|
/// <param name="tmpFile"></param>
|
||||||
|
/// <param name="imageSize"></param>
|
||||||
private static void InsertIntoExistingWorkbook(IWorkbook workbook, string tmpFile, Size imageSize) {
|
private static void InsertIntoExistingWorkbook(IWorkbook workbook, string tmpFile, Size imageSize) {
|
||||||
IWorksheet workSheet = workbook.ActiveSheet;
|
IWorksheet workSheet = workbook.ActiveSheet;
|
||||||
if (workSheet != null) {
|
if (workSheet == null) {
|
||||||
if (workSheet.Shapes != null) {
|
return;
|
||||||
IShape shape = workSheet.Shapes.AddPicture(tmpFile, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, imageSize.Width, imageSize.Height);
|
}
|
||||||
if (shape != null) {
|
using (IShapes shapes = workSheet.Shapes) {
|
||||||
shape.Top = 40;
|
if (shapes != null) {
|
||||||
shape.Left = 40;
|
using (IShape shape = shapes.AddPicture(tmpFile, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, imageSize.Width, imageSize.Height)) {
|
||||||
shape.LockAspectRatio = MsoTriState.msoTrue;
|
if (shape != null) {
|
||||||
shape.ScaleHeight(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft);
|
shape.Top = 40;
|
||||||
shape.ScaleWidth(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft);
|
shape.Left = 40;
|
||||||
|
shape.LockAspectRatio = MsoTriState.msoTrue;
|
||||||
|
shape.ScaleHeight(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft);
|
||||||
|
shape.ScaleWidth(1, MsoTriState.msoTrue, MsoScaleFrom.msoScaleFromTopLeft);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add an image-file to a newly created workbook
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tmpFile"></param>
|
||||||
|
/// <param name="imageSize"></param>
|
||||||
public static void InsertIntoNewWorkbook(string tmpFile, Size imageSize) {
|
public static void InsertIntoNewWorkbook(string tmpFile, Size imageSize) {
|
||||||
using (IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance<IExcelApplication>()) {
|
using (IExcelApplication excelApplication = GetOrCreateExcelApplication()) {
|
||||||
if (excelApplication != null) {
|
if (excelApplication != null) {
|
||||||
excelApplication.Visible = true;
|
excelApplication.Visible = true;
|
||||||
object template = Missing.Value;
|
object template = Missing.Value;
|
||||||
IWorkbook workbook = excelApplication.Workbooks.Add(template);
|
using (IWorkbooks workbooks = excelApplication.Workbooks) {
|
||||||
InsertIntoExistingWorkbook(workbook, tmpFile, imageSize);
|
IWorkbook workbook = workbooks.Add(template);
|
||||||
|
InsertIntoExistingWorkbook(workbook, tmpFile, imageSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Call this to get the running Excel application, returns null if there isn't any.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>IExcelApplication or null</returns>
|
||||||
|
private static IExcelApplication GetExcelApplication() {
|
||||||
|
IExcelApplication excelApplication = COMWrapper.GetInstance<IExcelApplication>();
|
||||||
|
InitializeVariables(excelApplication);
|
||||||
|
return excelApplication;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Call this to get the running Excel application, or create a new instance
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>IExcelApplication</returns>
|
||||||
|
private static IExcelApplication GetOrCreateExcelApplication() {
|
||||||
|
IExcelApplication excelApplication = COMWrapper.GetOrCreateInstance<IExcelApplication>();
|
||||||
|
InitializeVariables(excelApplication);
|
||||||
|
return excelApplication;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize static outlook variables like version and currentuser
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="excelApplication"></param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 readonly string SIGNATURE_PATH = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Microsoft\Signatures");
|
||||||
private static Version outlookVersion = null;
|
private static Version outlookVersion = null;
|
||||||
private static string currentUser = 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:
|
// 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]
|
||||||
|
@ -68,7 +64,7 @@ namespace Greenshot.Interop.Office {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outlookVersion.Major >= OUTLOOK_2013) {
|
if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2013) {
|
||||||
// Check inline "panel" for Outlook 2013
|
// Check inline "panel" for Outlook 2013
|
||||||
using (var activeExplorer = outlookApplication.ActiveExplorer()) {
|
using (var activeExplorer = outlookApplication.ActiveExplorer()) {
|
||||||
if (activeExplorer != null) {
|
if (activeExplorer != null) {
|
||||||
|
@ -120,7 +116,7 @@ namespace Greenshot.Interop.Office {
|
||||||
if (!mailItem.Sent) {
|
if (!mailItem.Sent) {
|
||||||
return true;
|
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<AppointmentItem>(currentItem);
|
//AppointmentItem appointmentItem = COMWrapper.Cast<AppointmentItem>(currentItem);
|
||||||
AppointmentItem appointmentItem = (AppointmentItem)currentItem;
|
AppointmentItem appointmentItem = (AppointmentItem)currentItem;
|
||||||
if (string.IsNullOrEmpty(appointmentItem.Organizer) || (currentUser != null && currentUser.Equals(appointmentItem.Organizer))) {
|
if (string.IsNullOrEmpty(appointmentItem.Organizer) || (currentUser != null && currentUser.Equals(appointmentItem.Organizer))) {
|
||||||
|
@ -149,7 +145,7 @@ namespace Greenshot.Interop.Office {
|
||||||
if (outlookApplication == null) {
|
if (outlookApplication == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (outlookVersion.Major >= OUTLOOK_2013) {
|
if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2013) {
|
||||||
// Check inline "panel" for Outlook 2013
|
// Check inline "panel" for Outlook 2013
|
||||||
using (var activeExplorer = outlookApplication.ActiveExplorer()) {
|
using (var activeExplorer = outlookApplication.ActiveExplorer()) {
|
||||||
if (activeExplorer == null) {
|
if (activeExplorer == null) {
|
||||||
|
@ -271,7 +267,7 @@ namespace Greenshot.Interop.Office {
|
||||||
LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat);
|
LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat);
|
||||||
|
|
||||||
string contentID;
|
string contentID;
|
||||||
if (outlookVersion.Major >= OUTLOOK_2007) {
|
if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) {
|
||||||
contentID = Guid.NewGuid().ToString();
|
contentID = Guid.NewGuid().ToString();
|
||||||
} else {
|
} else {
|
||||||
LOG.Info("Older Outlook (<2007) found, using filename as contentid.");
|
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!)
|
// 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)) {
|
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
|
// Add the content id to the attachment, this only works for Outlook >= 2007
|
||||||
try {
|
try {
|
||||||
IPropertyAccessor propertyAccessor = attachment.PropertyAccessor;
|
IPropertyAccessor propertyAccessor = attachment.PropertyAccessor;
|
||||||
|
@ -386,7 +382,7 @@ namespace Greenshot.Interop.Office {
|
||||||
// Create the attachment (and dispose the COM object after using)
|
// Create the attachment (and dispose the COM object after using)
|
||||||
using (IAttachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) {
|
using (IAttachment attachment = newMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, 0, attachmentName)) {
|
||||||
// add content ID to the attachment
|
// add content ID to the attachment
|
||||||
if (outlookVersion.Major >= OUTLOOK_2007) {
|
if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) {
|
||||||
try {
|
try {
|
||||||
contentID = Guid.NewGuid().ToString();
|
contentID = Guid.NewGuid().ToString();
|
||||||
IPropertyAccessor propertyAccessor = attachment.PropertyAccessor;
|
IPropertyAccessor propertyAccessor = attachment.PropertyAccessor;
|
||||||
|
@ -526,11 +522,11 @@ namespace Greenshot.Interop.Office {
|
||||||
LOG.InfoFormat("Using Outlook {0}", outlookVersion);
|
LOG.InfoFormat("Using Outlook {0}", outlookVersion);
|
||||||
} catch (Exception exVersion) {
|
} catch (Exception exVersion) {
|
||||||
LOG.Error(exVersion);
|
LOG.Error(exVersion);
|
||||||
LOG.Warn("Assuming outlook version 1.");
|
LOG.Warn("Assuming outlook version 1997.");
|
||||||
outlookVersion = new Version(1, 1, 1, 1);
|
outlookVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0);
|
||||||
}
|
}
|
||||||
// Preventing retrieval of currentUser if Outlook is older than 2007
|
// Preventing retrieval of currentUser if Outlook is older than 2007
|
||||||
if (outlookVersion.Major >= OUTLOOK_2007) {
|
if (outlookVersion.Major >= (int)OfficeVersion.OFFICE_2007) {
|
||||||
try {
|
try {
|
||||||
INameSpace mapiNamespace = outlookApplication.GetNameSpace("MAPI");
|
INameSpace mapiNamespace = outlookApplication.GetNameSpace("MAPI");
|
||||||
currentUser = mapiNamespace.CurrentUser.Name;
|
currentUser = mapiNamespace.CurrentUser.Name;
|
||||||
|
|
|
@ -28,14 +28,12 @@ using Greenshot.Interop;
|
||||||
namespace Greenshot.Interop.Office {
|
namespace Greenshot.Interop.Office {
|
||||||
public class PowerpointExporter {
|
public class PowerpointExporter {
|
||||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(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() {
|
public static bool isAfter2003() {
|
||||||
if (version != null) {
|
return powerpointVersion.Major > (int)OfficeVersion.OFFICE_2003;
|
||||||
return !version.StartsWith("11");
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the captions of all the open powerpoint presentations
|
/// Get the captions of all the open powerpoint presentations
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -43,13 +41,11 @@ namespace Greenshot.Interop.Office {
|
||||||
public static List<string> GetPowerpointPresentations() {
|
public static List<string> GetPowerpointPresentations() {
|
||||||
List<string> foundPresentations = new System.Collections.Generic.List<string>();
|
List<string> foundPresentations = new System.Collections.Generic.List<string>();
|
||||||
try {
|
try {
|
||||||
using (IPowerpointApplication powerpointApplication = COMWrapper.GetInstance<IPowerpointApplication>()) {
|
using (IPowerpointApplication powerpointApplication = GetPowerpointApplication()) {
|
||||||
if (powerpointApplication == null) {
|
if (powerpointApplication == null) {
|
||||||
return foundPresentations;
|
return foundPresentations;
|
||||||
}
|
}
|
||||||
if (version == null) {
|
|
||||||
version = powerpointApplication.Version;
|
|
||||||
}
|
|
||||||
using (IPresentations presentations = powerpointApplication.Presentations) {
|
using (IPresentations presentations = powerpointApplication.Presentations) {
|
||||||
LOG.DebugFormat("Open Presentations: {0}", presentations.Count);
|
LOG.DebugFormat("Open Presentations: {0}", presentations.Count);
|
||||||
for (int i = 1; i <= presentations.Count; i++) {
|
for (int i = 1; i <= presentations.Count; i++) {
|
||||||
|
@ -86,7 +82,7 @@ namespace Greenshot.Interop.Office {
|
||||||
/// <param name="title">A string with the image title</param>
|
/// <param name="title">A string with the image title</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool ExportToPresentation(string presentationName, string tmpFile, Size imageSize, string title) {
|
public static bool ExportToPresentation(string presentationName, string tmpFile, Size imageSize, string title) {
|
||||||
using (IPowerpointApplication powerpointApplication = COMWrapper.GetInstance<IPowerpointApplication>()) {
|
using (IPowerpointApplication powerpointApplication = GetPowerpointApplication()) {
|
||||||
if (powerpointApplication == null) {
|
if (powerpointApplication == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -221,7 +217,7 @@ namespace Greenshot.Interop.Office {
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool InsertIntoNewPresentation(string tmpFile, Size imageSize, string title) {
|
public static bool InsertIntoNewPresentation(string tmpFile, Size imageSize, string title) {
|
||||||
bool isPictureAdded = false;
|
bool isPictureAdded = false;
|
||||||
using (IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance<IPowerpointApplication>()) {
|
using (IPowerpointApplication powerpointApplication = GetOrCreatePowerpointApplication()) {
|
||||||
if (powerpointApplication == null) {
|
if (powerpointApplication == null) {
|
||||||
return isPictureAdded;
|
return isPictureAdded;
|
||||||
}
|
}
|
||||||
|
@ -239,5 +235,44 @@ namespace Greenshot.Interop.Office {
|
||||||
}
|
}
|
||||||
return isPictureAdded;
|
return isPictureAdded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Call this to get the running powerpoint application, returns null if there isn't any.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>IPowerpointApplication or null</returns>
|
||||||
|
private static IPowerpointApplication GetPowerpointApplication() {
|
||||||
|
IPowerpointApplication powerpointApplication = COMWrapper.GetInstance<IPowerpointApplication>();
|
||||||
|
InitializeVariables(powerpointApplication);
|
||||||
|
return powerpointApplication;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Call this to get the running powerpoint application, or create a new instance
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>IPowerpointApplication</returns>
|
||||||
|
private static IPowerpointApplication GetOrCreatePowerpointApplication() {
|
||||||
|
IPowerpointApplication powerpointApplication = COMWrapper.GetOrCreateInstance<IPowerpointApplication>();
|
||||||
|
InitializeVariables(powerpointApplication);
|
||||||
|
return powerpointApplication;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize static outlook variables like version and currentuser
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="powerpointApplication">IPowerpointApplication</param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,15 +29,17 @@ using Greenshot.IniFile;
|
||||||
namespace Greenshot.Interop.Office {
|
namespace Greenshot.Interop.Office {
|
||||||
public class WordExporter {
|
public class WordExporter {
|
||||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(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<OfficeConfiguration>();
|
private static OfficeConfiguration config = IniConfig.GetIniSection<OfficeConfiguration>();
|
||||||
|
|
||||||
public static bool isAfter2003() {
|
/// <summary>
|
||||||
if (version != null) {
|
/// Check if the used version is higher than Office 2003
|
||||||
return !version.StartsWith("11");
|
/// </summary>
|
||||||
}
|
/// <returns></returns>
|
||||||
return false;
|
private static bool isAfter2003() {
|
||||||
|
return wordVersion.Major > (int)OfficeVersion.OFFICE_2003;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Insert the bitmap stored under the tempfile path into the word document with the supplied caption
|
/// Insert the bitmap stored under the tempfile path into the word document with the supplied caption
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -45,12 +47,17 @@ namespace Greenshot.Interop.Office {
|
||||||
/// <param name="tmpFile"></param>
|
/// <param name="tmpFile"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool InsertIntoExistingDocument(string wordCaption, string tmpFile) {
|
public static bool InsertIntoExistingDocument(string wordCaption, string tmpFile) {
|
||||||
using (IWordApplication wordApplication = COMWrapper.GetInstance<IWordApplication>()) {
|
using (IWordApplication wordApplication = GetWordApplication()) {
|
||||||
if (wordApplication != null) {
|
if (wordApplication == null) {
|
||||||
for (int i = 1; i <= wordApplication.Documents.Count; i++) {
|
return false;
|
||||||
using (IWordDocument wordDocument = wordApplication.Documents.item(i)) {
|
}
|
||||||
if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) {
|
using (IDocuments documents = wordApplication.Documents) {
|
||||||
return InsertIntoExistingDocument(wordApplication, wordDocument, tmpFile, null, null);
|
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();
|
wordDocument.Activate();
|
||||||
} catch {
|
} 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
|
// Add Picture
|
||||||
using (IInlineShape shape = AddPictureToSelection(wordApplication.Selection, tmpFile)) {
|
using (IInlineShape shape = AddPictureToSelection(selection, tmpFile)) {
|
||||||
if (!string.IsNullOrEmpty(address)) {
|
if (!string.IsNullOrEmpty(address)) {
|
||||||
object screentip = Type.Missing;
|
object screentip = Type.Missing;
|
||||||
if (!string.IsNullOrEmpty(tooltip)) {
|
if (!string.IsNullOrEmpty(tooltip)) {
|
||||||
screentip = tooltip;
|
screentip = tooltip;
|
||||||
}
|
}
|
||||||
try {
|
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) {
|
} catch (Exception e) {
|
||||||
LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message);
|
LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
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) {
|
} catch (Exception e) {
|
||||||
if (e.InnerException != null) {
|
if (e.InnerException != null) {
|
||||||
LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.InnerException.Message);
|
LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.InnerException.Message);
|
||||||
|
@ -101,64 +121,74 @@ namespace Greenshot.Interop.Office {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
wordApplication.Activate();
|
wordApplication.Activate();
|
||||||
} catch {}
|
} catch {
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
wordDocument.Activate();
|
wordDocument.Activate();
|
||||||
} catch {}
|
} catch {
|
||||||
try {
|
}
|
||||||
wordDocument.ActiveWindow.Activate();
|
|
||||||
} catch {}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Helper method to add the file as image to the selection
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="selection"></param>
|
||||||
|
/// <param name="tmpFile"></param>
|
||||||
|
/// <returns></returns>
|
||||||
private static IInlineShape AddPictureToSelection(ISelection selection, string tmpFile) {
|
private static IInlineShape AddPictureToSelection(ISelection selection, string tmpFile) {
|
||||||
IInlineShape shape = selection.InlineShapes.AddPicture(tmpFile, false, true, Type.Missing);
|
using (IInlineShapes shapes = selection.InlineShapes) {
|
||||||
// Lock aspect ratio
|
IInlineShape shape = shapes.AddPicture(tmpFile, false, true, Type.Missing);
|
||||||
if (config.WordLockAspectRatio) {
|
// Lock aspect ratio
|
||||||
shape.LockAspectRatio = MsoTriState.msoTrue;
|
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) {
|
public static void InsertIntoNewDocument(string tmpFile, string address, string tooltip) {
|
||||||
using (IWordApplication wordApplication = COMWrapper.GetOrCreateInstance<IWordApplication>()) {
|
using (IWordApplication wordApplication = GetOrCreateWordApplication()) {
|
||||||
if (wordApplication != null) {
|
if (wordApplication == null) {
|
||||||
wordApplication.Visible = true;
|
return;
|
||||||
wordApplication.Activate();
|
}
|
||||||
// Create new Document
|
wordApplication.Visible = true;
|
||||||
object template = string.Empty;
|
wordApplication.Activate();
|
||||||
object newTemplate = false;
|
// Create new Document
|
||||||
object documentType = 0;
|
object template = string.Empty;
|
||||||
object documentVisible = true;
|
object newTemplate = false;
|
||||||
IWordDocument wordDocument = wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible);
|
object documentType = 0;
|
||||||
// Add Picture
|
object documentVisible = true;
|
||||||
using (IInlineShape shape = AddPictureToSelection(wordApplication.Selection, tmpFile)) {
|
using (IDocuments documents = wordApplication.Documents) {
|
||||||
if (!string.IsNullOrEmpty(address)) {
|
using (IWordDocument wordDocument = documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible)) {
|
||||||
object screentip = Type.Missing;
|
using (ISelection selection = wordApplication.Selection) {
|
||||||
if (!string.IsNullOrEmpty(tooltip)) {
|
// Add Picture
|
||||||
screentip = tooltip;
|
using (IInlineShape shape = AddPictureToSelection(selection, tmpFile)) {
|
||||||
}
|
if (!string.IsNullOrEmpty(address)) {
|
||||||
try {
|
object screentip = Type.Missing;
|
||||||
wordDocument.Hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing);
|
if (!string.IsNullOrEmpty(tooltip)) {
|
||||||
} catch (Exception e) {
|
screentip = tooltip;
|
||||||
LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message);
|
}
|
||||||
|
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 {
|
||||||
try {
|
wordDocument.Activate();
|
||||||
wordApplication.Activate();
|
} catch {
|
||||||
} catch {
|
}
|
||||||
}
|
try {
|
||||||
try {
|
wordDocument.ActiveWindow.Activate();
|
||||||
wordDocument.Activate();
|
} catch {
|
||||||
} catch {
|
}
|
||||||
}
|
|
||||||
try {
|
|
||||||
wordDocument.ActiveWindow.Activate();
|
|
||||||
} catch {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,31 +199,72 @@ namespace Greenshot.Interop.Office {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static List<string> GetWordDocuments() {
|
public static List<string> GetWordDocuments() {
|
||||||
List<string> documents = new List<string>();
|
List<string> openDocuments = new List<string>();
|
||||||
try {
|
try {
|
||||||
using (IWordApplication wordApplication = COMWrapper.GetInstance<IWordApplication>()) {
|
using (IWordApplication wordApplication = GetWordApplication()) {
|
||||||
if (wordApplication != null) {
|
if (wordApplication == null) {
|
||||||
if (version == null) {
|
return openDocuments;
|
||||||
version = wordApplication.Version;
|
}
|
||||||
}
|
using (IDocuments documents = wordApplication.Documents) {
|
||||||
for (int i = 1; i <= wordApplication.Documents.Count; i++) {
|
for (int i = 1; i <= documents.Count; i++) {
|
||||||
IWordDocument document = wordApplication.Documents.item(i);
|
using (IWordDocument document = documents.item(i)) {
|
||||||
if (document.ReadOnly) {
|
if (document.ReadOnly) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (isAfter2003()) {
|
|
||||||
if (document.Final) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (isAfter2003()) {
|
||||||
|
if (document.Final) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
using (IWordWindow activeWindow = document.ActiveWindow) {
|
||||||
|
openDocuments.Add(activeWindow.Caption);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
documents.Add(document.ActiveWindow.Caption);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
|
LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
|
||||||
}
|
}
|
||||||
return documents;
|
return openDocuments;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Call this to get the running outlook application, returns null if there isn't any.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>IWordApplication or null</returns>
|
||||||
|
private static IWordApplication GetWordApplication() {
|
||||||
|
IWordApplication wordApplication = COMWrapper.GetInstance<IWordApplication>();
|
||||||
|
InitializeVariables(wordApplication);
|
||||||
|
return wordApplication;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Call this to get the running word application, or create a new instance
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>IWordApplication</returns>
|
||||||
|
private static IWordApplication GetOrCreateWordApplication() {
|
||||||
|
IWordApplication wordApplication = COMWrapper.GetOrCreateInstance<IWordApplication>();
|
||||||
|
InitializeVariables(wordApplication);
|
||||||
|
return wordApplication;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize static outlook variables like version and currentuser
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="wordApplication"></param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,38 +23,64 @@ namespace Greenshot.Interop.Office {
|
||||||
// See http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.application.aspx
|
// See http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.application.aspx
|
||||||
[ComProgId("Excel.Application")]
|
[ComProgId("Excel.Application")]
|
||||||
public interface IExcelApplication : ICommon {
|
public interface IExcelApplication : ICommon {
|
||||||
IWorkbook ActiveWorkbook { get; }
|
IWorkbook ActiveWorkbook {
|
||||||
|
get;
|
||||||
|
}
|
||||||
//ISelection Selection {get;}
|
//ISelection Selection {get;}
|
||||||
IWorkbooks Workbooks { get; }
|
IWorkbooks Workbooks {
|
||||||
bool Visible { get; set; }
|
get;
|
||||||
|
}
|
||||||
|
bool Visible {
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
string Version {
|
||||||
|
get;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.aspx
|
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.aspx
|
||||||
public interface IWorkbooks : ICommon, ICollection {
|
public interface IWorkbooks : ICommon, ICollection {
|
||||||
IWorkbook Add(object template);
|
IWorkbook Add(object template);
|
||||||
// Use index + 1!!
|
// 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
|
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbook.aspx
|
||||||
public interface IWorkbook : ICommon {
|
public interface IWorkbook : ICommon {
|
||||||
IWorksheet ActiveSheet { get; }
|
IWorksheet ActiveSheet {
|
||||||
string Name { get; }
|
get;
|
||||||
|
}
|
||||||
|
string Name {
|
||||||
|
get;
|
||||||
|
}
|
||||||
void Activate();
|
void Activate();
|
||||||
IWorksheets Worksheets { get; }
|
IWorksheets Worksheets {
|
||||||
|
get;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._worksheet_members.aspx
|
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._worksheet_members.aspx
|
||||||
public interface IWorksheet : ICommon {
|
public interface IWorksheet : ICommon {
|
||||||
IPictures Pictures { get; }
|
IPictures Pictures {
|
||||||
IShapes Shapes {get; }
|
get;
|
||||||
string Name { get; }
|
}
|
||||||
|
IShapes Shapes {
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
string Name {
|
||||||
|
get;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.iworksheets_members.aspx
|
// See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.iworksheets_members.aspx
|
||||||
public interface IWorksheets : ICommon, ICollection {
|
public interface IWorksheets : ICommon, ICollection {
|
||||||
// Use index + 1!!
|
// Use index + 1!!
|
||||||
IWorksheet this[object Index] { get; }
|
IWorksheet this[object Index] {
|
||||||
|
get;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IPictures : ICommon, ICollection {
|
public interface IPictures : ICommon, ICollection {
|
||||||
|
|
|
@ -21,11 +21,23 @@
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
|
||||||
namespace Greenshot.Interop.Office {
|
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
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If the "type" this[object index] { get; } is implemented, use index + 1!!! (starts at 1)
|
/// If the "type" this[object index] { get; } is implemented, use index + 1!!! (starts at 1)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ICollection : ICommon, IEnumerable {
|
public interface ICollection : ICommon, IEnumerable {
|
||||||
int Count { get; }
|
int Count {
|
||||||
|
get;
|
||||||
|
}
|
||||||
void Remove(int index);
|
void Remove(int index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue