mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-12-01 18:54:53 +01:00
Improve error handling in office converters
This commit is contained in:
1 parent
92c96f03e8
commit
c9b07e7162
8 files changed
+128
-20
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -546,6 +546,15 @@ namespace FileConverter.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Fail to open document with Microsoft Office (check if your licence/installation is valid)..
|
||||
/// </summary>
|
||||
public static string ErrorUnableToUseMicrosoftOffice {
|
||||
get {
|
||||
return ResourceManager.GetString("ErrorUnableToUseMicrosoftOffice", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Unsupported output format '{0}'..
|
||||
/// </summary>
|
||||
|
||||
@@ -529,4 +529,7 @@ use maj for uppercase version</value>
|
||||
<data name="ErrorMicrosoftOfficeIsNotAvailable" xml:space="preserve">
|
||||
<value>Microsoft Office must be installed in order to convert Office documents.</value>
|
||||
</data>
|
||||
<data name="ErrorUnableToUseMicrosoftOffice" xml:space="preserve">
|
||||
<value>Fail to open document with Microsoft Office (check if your licence/installation are valid).</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -547,4 +547,7 @@ utilisez maj pour une version en majuscules</value>
|
||||
<data name="ErrorMicrosoftOfficeIsNotAvailable" xml:space="preserve">
|
||||
<value>Microsoft Office doit être installé pour pouvoir convertir les documents Office.</value>
|
||||
</data>
|
||||
<data name="ErrorUnableToUseMicrosoftOffice" xml:space="preserve">
|
||||
<value>Impossible d'ouvrir le document avec Microsoft Office (vérifiez que la licence/l'installation sont valides).</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -529,4 +529,7 @@ use maj for uppercase version</value>
|
||||
<data name="ErrorMicrosoftOfficeIsNotAvailable" xml:space="preserve">
|
||||
<value>Microsoft Office must be installed in order to convert Office documents.</value>
|
||||
</data>
|
||||
<data name="ErrorUnableToUseMicrosoftOffice" xml:space="preserve">
|
||||
<value>Fail to open document with Microsoft Office (check if your licence/installation is valid).</value>
|
||||
</data>
|
||||
</root>
|
||||
Reference in new issue
Block a user