Fixed: Microsoft office applications detection

This commit is contained in:
Tichau committed 2016-10-10 18:42:20 +02:00
1 parent c9b07e7162
commit 92b5629956
10 files changed
+182 -35

No files matched your search

@@ -6,9 +6,10 @@ namespace FileConverter.ConversionJobs
using System.IO;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using FileConverter.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;
public class ConversionJob_Excel : ConversionJob_Office
{
private Excel.Workbook document;
@@ -25,6 +26,14 @@ namespace FileConverter.ConversionJobs
{
}
protected override ApplicationName Application
{
get
{
return ApplicationName.Excel;
}
}
protected override int GetOuputFilesCount()
{
if (this.ConversionPreset.OutputType == OutputType.Pdf)
@@ -12,14 +12,44 @@ namespace FileConverter.ConversionJobs
{
}
public enum ApplicationName
{
None,
Word,
Excel,
PowerPoint
}
protected abstract ApplicationName Application
{
get;
}
protected override void Initialize()
{
base.Initialize();
if (!Helpers.IsMicrosoftOfficeAvailable())
if (!Helpers.IsMicrosoftOfficeApplicationAvailable(this.Application))
{
this.ConversionFailed(Properties.Resources.ErrorMicrosoftOfficeIsNotAvailable);
return;
switch (this.Application)
{
case ApplicationName.Word:
this.ConversionFailed(Properties.Resources.ErrorMicrosoftWordIsNotAvailable);
return;
case ApplicationName.PowerPoint:
this.ConversionFailed(Properties.Resources.ErrorMicrosoftPowerPointIsNotAvailable);
return;
case ApplicationName.Excel:
this.ConversionFailed(Properties.Resources.ErrorMicrosoftExcelIsNotAvailable);
return;
default:
this.ConversionFailed(Properties.Resources.ErrorMicrosoftOfficeIsNotAvailable);
return;
}
}
}
@@ -6,9 +6,10 @@ namespace FileConverter.ConversionJobs
using System.IO;
using System.Threading.Tasks;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using FileConverter.Diagnostics;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
public class ConversionJob_PowerPoint : ConversionJob_Office
{
@@ -26,6 +27,14 @@ namespace FileConverter.ConversionJobs
{
}
protected override ApplicationName Application
{
get
{
return ApplicationName.PowerPoint;
}
}
protected override int GetOuputFilesCount()
{
if (this.ConversionPreset.OutputType == OutputType.Pdf)
@@ -6,9 +6,10 @@ namespace FileConverter.ConversionJobs
using System.IO;
using System.Threading.Tasks;
using Word = Microsoft.Office.Interop.Word;
using FileConverter.Diagnostics;
using Word = Microsoft.Office.Interop.Word;
public class ConversionJob_Word : ConversionJob_Office
{
private Word.Document document;
@@ -25,6 +26,14 @@ namespace FileConverter.ConversionJobs
{
}
protected override ApplicationName Application
{
get
{
return ApplicationName.Word;
}
}
protected override int GetOuputFilesCount()
{
if (this.ConversionPreset.OutputType == OutputType.Pdf)
+46 -26
View File
@@ -10,23 +10,11 @@ namespace FileConverter
using System.Reflection;
using System.Threading;
using FileConverter.ConversionJobs;
using Microsoft.Win32;
public static class Helpers
{
private static readonly string[] CompatibleOfficeVersions = new string[]
{
"16.0", // Office 2016
"15.0", // Office 2013
"14.0", // Office 2010
"12.0", // Office 2007
"11.0", // Office 2003
"10.0", // Office XP
"9.0", // Office 2000
"8.0", // Office 98
"7.0", // Office 97
};
public static IEnumerable<CultureInfo> GetSupportedCultures()
{
// Get all cultures.
@@ -184,42 +172,74 @@ namespace FileConverter
/// </summary>
/// <returns>Returns true if Office is installed on the computer.</returns>
/// source: http://stackoverflow.com/questions/3266675/how-to-detect-installed-version-of-ms-office/3267832#3267832
public static bool IsMicrosoftOfficeAvailable()
/// source: http://www.codeproject.com/Articles/26520/Getting-Office-s-Version
public static bool IsMicrosoftOfficeApplicationAvailable(ConversionJobs.ConversionJob_Office.ApplicationName application)
{
string registryKeyPattern = @"Software\Microsoft\Office\{0}\Common";
for (int index = 0; index < Helpers.CompatibleOfficeVersions.Length; index++)
string registryKeyPattern = @"Software\Microsoft\Windows\CurrentVersion\App Paths\";
switch (application)
{
string registryKeyPath = string.Format(registryKeyPattern, Helpers.CompatibleOfficeVersions[index]);
RegistryKey officeRegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(registryKeyPath);
if (officeRegistryKey != null)
case ConversionJob_Office.ApplicationName.Word:
registryKeyPattern += "winword.exe";
break;
case ConversionJob_Office.ApplicationName.PowerPoint:
registryKeyPattern += "powerpnt.exe";
break;
case ConversionJob_Office.ApplicationName.Excel:
registryKeyPattern += "excel.exe";
break;
case ConversionJob_Office.ApplicationName.None:
return false;
}
// Looks inside CURRENT_USER.
RegistryKey winwordKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(registryKeyPattern, false);
if (winwordKey != null)
{
string winwordPath = winwordKey.GetValue(string.Empty).ToString();
if (!string.IsNullOrEmpty(winwordPath))
{
return true;
}
}
// If not found, looks inside LOCAL_MACHINE.
winwordKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(registryKeyPattern, false);
if (winwordKey != null)
{
string winwordPath = winwordKey.GetValue(string.Empty).ToString();
if (!string.IsNullOrEmpty(winwordPath))
{
Diagnostics.Debug.Log($"Registry key '{registryKeyPath}' found");
return true;
}
}
Diagnostics.Debug.Log("No compatible office version found.");
return false;
}
public static bool IsExtensionCompatibleWithOffice(string extension)
public static ConversionJob_Office.ApplicationName GetOfficeApplicationCompatibleWithExtension(string extension)
{
switch (extension)
{
case "doc":
case "docx":
case "odt":
return ConversionJob_Office.ApplicationName.Word;
case "ppt":
case "pptx":
case "odp":
return ConversionJob_Office.ApplicationName.PowerPoint;
case "ods":
case "odt":
case "xls":
case "xlsx":
return true;
return ConversionJob_Office.ApplicationName.Excel;
}
return false;
return ConversionJob_Office.ApplicationName.None;
}
public static class InputCategoryNames
@@ -537,6 +537,15 @@ namespace FileConverter.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Microsoft Excel must be installed in order to convert Excel documents..
/// </summary>
public static string ErrorMicrosoftExcelIsNotAvailable {
get {
return ResourceManager.GetString("ErrorMicrosoftExcelIsNotAvailable", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Microsoft Office must be installed in order to convert Office documents..
/// </summary>
@@ -546,6 +555,24 @@ namespace FileConverter.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Microsoft PowerPoint must be installed in order to convert PowerPoint documents..
/// </summary>
public static string ErrorMicrosoftPowerPointIsNotAvailable {
get {
return ResourceManager.GetString("ErrorMicrosoftPowerPointIsNotAvailable", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Microsoft Word must be installed in order to convert Word documents..
/// </summary>
public static string ErrorMicrosoftWordIsNotAvailable {
get {
return ResourceManager.GetString("ErrorMicrosoftWordIsNotAvailable", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Fail to open document with Microsoft Office (check if your licence/installation is valid)..
/// </summary>
@@ -532,4 +532,13 @@ use maj for uppercase version</value>
<data name="ErrorUnableToUseMicrosoftOffice" xml:space="preserve">
<value>Fail to open document with Microsoft Office (check if your licence/installation are valid).</value>
</data>
<data name="ErrorMicrosoftExcelIsNotAvailable" xml:space="preserve">
<value>Microsoft Excel must be installed in order to convert Excel documents.</value>
</data>
<data name="ErrorMicrosoftPowerPointIsNotAvailable" xml:space="preserve">
<value>Microsoft PowerPoint must be installed in order to convert PowerPoint documents.</value>
</data>
<data name="ErrorMicrosoftWordIsNotAvailable" xml:space="preserve">
<value>Microsoft Word must be installed in order to convert Word documents.</value>
</data>
</root>
@@ -550,4 +550,13 @@ utilisez maj pour une version en majuscules</value>
<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>
<data name="ErrorMicrosoftExcelIsNotAvailable" xml:space="preserve">
<value>Microsoft Excel doit être installé pour pouvoir convertir les documents Excel.</value>
</data>
<data name="ErrorMicrosoftPowerPointIsNotAvailable" xml:space="preserve">
<value>Microsoft PowerPoint doit être installé pour pouvoir convertir les documents PowerPoint.</value>
</data>
<data name="ErrorMicrosoftWordIsNotAvailable" xml:space="preserve">
<value>Microsoft Word doit être installé pour pouvoir convertir les documents Word.</value>
</data>
</root>
@@ -532,4 +532,13 @@ use maj for uppercase version</value>
<data name="ErrorUnableToUseMicrosoftOffice" xml:space="preserve">
<value>Fail to open document with Microsoft Office (check if your licence/installation is valid).</value>
</data>
<data name="ErrorMicrosoftExcelIsNotAvailable" xml:space="preserve">
<value>Microsoft Excel must be installed in order to convert Excel documents.</value>
</data>
<data name="ErrorMicrosoftPowerPointIsNotAvailable" xml:space="preserve">
<value>Microsoft PowerPoint must be installed in order to convert PowerPoint documents.</value>
</data>
<data name="ErrorMicrosoftWordIsNotAvailable" xml:space="preserve">
<value>Microsoft Word must be installed in order to convert Word documents.</value>
</data>
</root>
@@ -7,6 +7,7 @@ namespace FileConverter
using System.Windows.Media;
using FileConverter.Annotations;
using FileConverter.ConversionJobs;
public class InputExtension : INotifyPropertyChanged
{
@@ -21,14 +22,29 @@ namespace FileConverter
{
this.Name = name;
if (!Helpers.IsExtensionCompatibleWithOffice(name) || Helpers.IsMicrosoftOfficeAvailable())
ConversionJob_Office.ApplicationName officeApplication = Helpers.GetOfficeApplicationCompatibleWithExtension(name);
if (officeApplication == ConversionJob_Office.ApplicationName.None || Helpers.IsMicrosoftOfficeApplicationAvailable(officeApplication))
{
this.ForegroundBrush = this.defaultBrush;
}
else
{
this.ForegroundBrush = this.errorBrush;
this.ToolTip = Properties.Resources.ErrorMicrosoftOfficeIsNotAvailable;
switch (officeApplication)
{
case ConversionJob_Office.ApplicationName.Word:
this.ToolTip = Properties.Resources.ErrorMicrosoftWordIsNotAvailable;
break;
case ConversionJob_Office.ApplicationName.PowerPoint:
this.ToolTip = Properties.Resources.ErrorMicrosoftPowerPointIsNotAvailable;
break;
case ConversionJob_Office.ApplicationName.Excel:
this.ToolTip = Properties.Resources.ErrorMicrosoftExcelIsNotAvailable;
break;
}
}
}