diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs index c1bd513..b0d6ec1 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs @@ -2,10 +2,12 @@ namespace FileConverter.ConversionJobs { + using System; using System.IO; using System.Threading.Tasks; using Excel = Microsoft.Office.Interop.Excel; + using FileConverter.Diagnostics; public class ConversionJob_Excel : ConversionJob_Office { @@ -30,7 +32,10 @@ namespace FileConverter.ConversionJobs return 1; } - this.LoadDocumentIfNecessary(); + if (!this.TryLoadDocumentIfNecessary()) + { + return 1; + } int pagesCount = 0; foreach (object sheet in this.document.Sheets) @@ -86,7 +91,11 @@ namespace FileConverter.ConversionJobs this.UserState = Properties.Resources.ConversionStateReadDocument; - this.LoadDocumentIfNecessary(); + if (!this.TryLoadDocumentIfNecessary()) + { + this.ConversionFailed(Properties.Resources.ErrorUnableToUseMicrosoftOffice); + return; + } // Make this document the active document. this.document.Activate(); @@ -150,8 +159,14 @@ namespace FileConverter.ConversionJobs protected override void ReleaseOfficeApplicationInstanceIfNeeded() { + if (this.application == null) + { + return; + } + Diagnostics.Debug.Log("Quit excel application via interop."); this.application.Quit(); + this.application = null; } private async Task UpdateProgress() @@ -173,10 +188,23 @@ namespace FileConverter.ConversionJobs await Task.Delay(40); } } - - private void LoadDocumentIfNecessary() + + private bool TryLoadDocumentIfNecessary() { - this.InitializeOfficeApplicationInstanceIfNecessary(); + try + { + this.InitializeOfficeApplicationInstanceIfNecessary(); + } + catch (Exception exception) + { + Debug.Log(exception.ToString()); + Debug.Log("Failed to initialize office application."); + } + + if (this.application == null) + { + return false; + } if (this.document == null) { @@ -184,6 +212,8 @@ namespace FileConverter.ConversionJobs this.document = this.application.Workbooks.Open(this.InputFilePath, System.Reflection.Missing.Value, true); } + + return this.document != null; } } } diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs b/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs index 619025f..5d19d42 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_PowerPoint.cs @@ -2,11 +2,13 @@ namespace FileConverter.ConversionJobs { + using System; using System.IO; using System.Threading.Tasks; using Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint; + using FileConverter.Diagnostics; public class ConversionJob_PowerPoint : ConversionJob_Office { @@ -31,7 +33,10 @@ namespace FileConverter.ConversionJobs return 1; } - this.LoadDocumentIfNecessary(); + if (!this.TryLoadDocumentIfNecessary()) + { + return 1; + } int pagesCount = this.document.Slides.Count; return pagesCount; @@ -78,7 +83,11 @@ namespace FileConverter.ConversionJobs this.UserState = Properties.Resources.ConversionStateReadDocument; - this.LoadDocumentIfNecessary(); + if (!this.TryLoadDocumentIfNecessary()) + { + this.ConversionFailed(Properties.Resources.ErrorUnableToUseMicrosoftOffice); + return; + } this.UserState = Properties.Resources.ConversionStateConversion; @@ -136,8 +145,14 @@ namespace FileConverter.ConversionJobs protected override void ReleaseOfficeApplicationInstanceIfNeeded() { + if (this.application == null) + { + return; + } + Diagnostics.Debug.Log("Quit PowerPoint application via interop."); this.application.Quit(); + this.application = null; } private async Task UpdateProgress() @@ -159,10 +174,23 @@ namespace FileConverter.ConversionJobs await Task.Delay(40); } } - - private void LoadDocumentIfNecessary() + + private bool TryLoadDocumentIfNecessary() { - this.InitializeOfficeApplicationInstanceIfNecessary(); + try + { + this.InitializeOfficeApplicationInstanceIfNecessary(); + } + catch (Exception exception) + { + Debug.Log(exception.ToString()); + Debug.Log("Failed to initialize office application."); + } + + if (this.application == null) + { + return false; + } if (this.document == null) { @@ -170,6 +198,8 @@ namespace FileConverter.ConversionJobs this.document = this.application.Presentations.Open(this.InputFilePath, ReadOnly: MsoTriState.msoTrue, WithWindow: MsoTriState.msoFalse); } + + return this.document != null; } } } diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs index 27acaed..dd7e7d2 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs @@ -2,15 +2,17 @@ namespace FileConverter.ConversionJobs { + using System; using System.IO; using System.Threading.Tasks; using Word = Microsoft.Office.Interop.Word; + using FileConverter.Diagnostics; public class ConversionJob_Word : ConversionJob_Office { private Word.Document document; - private Word.Application wordApplication; + private Word.Application application; private string intermediateFilePath = string.Empty; private ConversionJob pdf2ImageConversionJob = null; @@ -30,7 +32,10 @@ namespace FileConverter.ConversionJobs return 1; } - this.LoadDocumentIfNecessary(); + if (!this.TryLoadDocumentIfNecessary()) + { + return 1; + } int pagesCount = this.document.ComputeStatistics(Word.WdStatistic.wdStatisticPages); @@ -78,7 +83,11 @@ namespace FileConverter.ConversionJobs this.UserState = Properties.Resources.ConversionStateReadDocument; - this.LoadDocumentIfNecessary(); + if (!this.TryLoadDocumentIfNecessary()) + { + this.ConversionFailed(Properties.Resources.ErrorUnableToUseMicrosoftOffice); + return; + } // Make this document the active document. this.document.Activate(); @@ -127,14 +136,14 @@ namespace FileConverter.ConversionJobs protected override void InitializeOfficeApplicationInstanceIfNecessary() { - if (this.wordApplication != null) + if (this.application != null) { return; } // Initialize word application. Diagnostics.Debug.Log("Instantiate word application via interop."); - this.wordApplication = new Microsoft.Office.Interop.Word.Application + this.application = new Microsoft.Office.Interop.Word.Application { Visible = false }; @@ -142,8 +151,14 @@ namespace FileConverter.ConversionJobs protected override void ReleaseOfficeApplicationInstanceIfNeeded() { + if (this.application == null) + { + return; + } + Diagnostics.Debug.Log("Quit word application via interop."); - this.wordApplication.Quit(); + this.application.Quit(); + this.application = null; } private async Task UpdateProgress() @@ -166,16 +181,31 @@ namespace FileConverter.ConversionJobs } } - private void LoadDocumentIfNecessary() + private bool TryLoadDocumentIfNecessary() { - this.InitializeOfficeApplicationInstanceIfNecessary(); + try + { + this.InitializeOfficeApplicationInstanceIfNecessary(); + } + catch (Exception exception) + { + Debug.Log(exception.ToString()); + Debug.Log("Failed to initialize office application."); + } + + if (this.application == null) + { + return false; + } if (this.document == null) { Diagnostics.Debug.Log("Load word document '{0}'.", this.InputFilePath); - this.document = this.wordApplication.Documents.Open(this.InputFilePath, System.Reflection.Missing.Value, true); + this.document = this.application.Documents.Open(this.InputFilePath, System.Reflection.Missing.Value, true); } + + return this.document != null; } } } diff --git a/Application/FileConverter/Diagnostics/DiagnosticsData.cs b/Application/FileConverter/Diagnostics/DiagnosticsData.cs index ac2efc6..1507bcb 100644 --- a/Application/FileConverter/Diagnostics/DiagnosticsData.cs +++ b/Application/FileConverter/Diagnostics/DiagnosticsData.cs @@ -77,7 +77,7 @@ namespace FileConverter.Diagnostics public void Log(string message, params object[] arguments) { - string log = string.Format(message, arguments); + string log = arguments.Length > 0 ? string.Format(message, arguments) : message; this.logMessages.Add(log); this.logFileWriter.WriteLine(log); diff --git a/Application/FileConverter/Properties/Resources.Designer.cs b/Application/FileConverter/Properties/Resources.Designer.cs index dec3e86..9f68979 100644 --- a/Application/FileConverter/Properties/Resources.Designer.cs +++ b/Application/FileConverter/Properties/Resources.Designer.cs @@ -546,6 +546,15 @@ namespace FileConverter.Properties { } } + /// + /// Looks up a localized string similar to Fail to open document with Microsoft Office (check if your licence/installation is valid).. + /// + public static string ErrorUnableToUseMicrosoftOffice { + get { + return ResourceManager.GetString("ErrorUnableToUseMicrosoftOffice", resourceCulture); + } + } + /// /// Looks up a localized string similar to Unsupported output format '{0}'.. /// diff --git a/Application/FileConverter/Properties/Resources.en.resx b/Application/FileConverter/Properties/Resources.en.resx index 834ae0d..f27bf53 100644 --- a/Application/FileConverter/Properties/Resources.en.resx +++ b/Application/FileConverter/Properties/Resources.en.resx @@ -529,4 +529,7 @@ use maj for uppercase version Microsoft Office must be installed in order to convert Office documents. + + Fail to open document with Microsoft Office (check if your licence/installation are valid). + \ No newline at end of file diff --git a/Application/FileConverter/Properties/Resources.fr.resx b/Application/FileConverter/Properties/Resources.fr.resx index 1edd951..ea24a33 100644 --- a/Application/FileConverter/Properties/Resources.fr.resx +++ b/Application/FileConverter/Properties/Resources.fr.resx @@ -547,4 +547,7 @@ utilisez maj pour une version en majuscules Microsoft Office doit être installé pour pouvoir convertir les documents Office. + + Impossible d'ouvrir le document avec Microsoft Office (vérifiez que la licence/l'installation sont valides). + \ No newline at end of file diff --git a/Application/FileConverter/Properties/Resources.resx b/Application/FileConverter/Properties/Resources.resx index 5934303..ad8c030 100644 --- a/Application/FileConverter/Properties/Resources.resx +++ b/Application/FileConverter/Properties/Resources.resx @@ -529,4 +529,7 @@ use maj for uppercase version Microsoft Office must be installed in order to convert Office documents. + + Fail to open document with Microsoft Office (check if your licence/installation is valid). + \ No newline at end of file