Reputation: 36547
Is it possible to determine whether Excel is running in 32-bit or 64-bit from the Microsoft.Office.Interop.Excel.ApplicationClass?
Edit
The solution should work for both Excel 2010 and Excel 2007
Upvotes: 5
Views: 4262
Reputation: 1
Maybe this could work (do it for me with Excel 2013 ff.)
try
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
object excelInstance = Activator.CreateInstance(officeType);
if (excelInstance != null)
{
string results = officeType.InvokeMember("OperatingSystem", BindingFlags.GetProperty, null, excelInstance, null).ToString();
if (!string.IsNullOrEmpty(results))
detectedExcelPlatform = results.Contains("64")?EDetectExcelPlattform.Force64Bit:EDetectExcelPlattform.Force32Bit;
officeType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, excelInstance, null);
}
}
catch
{
// To Ignore
}
EDetectExcelPlattform doesn't matter, because it is only from my own code. Can be replaced with bool result.
Upvotes: 0
Reputation: 138925
This code should give you the "bitness" of Excel.
Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (System.Runtime.InteropServices.Marshal.SizeOf(app.HinstancePtr) == 8)
{
// excel 64-bit
}
else
{
// excel 32-bit
}
EDIT: here is another version that should work for previous versions of Excel as well. Just pass an ApplicationClass reference to it:
public static ExcelVersion GetExcelVersion(object applicationClass)
{
if (applicationClass == null)
throw new ArgumentNullException("applicationClass");
PropertyInfo property = applicationClass.GetType().GetProperty("HinstancePtr", BindingFlags.Instance | BindingFlags.Public);
if (property == null)
return ExcelVersion.Excel;
return (System.Runtime.InteropServices.Marshal.SizeOf(property.GetValue(applicationClass, null)) == 8) ? ExcelVersion.Excel2010_64 : ExcelVersion.Excel2010_32;
}
public enum ExcelVersion
{
Excel, // before 2010, so 32 bits
Excel2010_32,
Excel2010_64
}
Upvotes: 9