I have an application that is exporting some data in an excel file. The problem is when the export is ran on a machine
that doesn't have Excel 2003 installed. When I try to export on a
machine that has office Xp installed i have the error below, even if i
test the version of excel installed. I don't understand why it tries to
find the office dll, even if I don't try to export. And the error
happens only if I press the export button, it doesn't happen on other
buttons on the form. And it seems that it doesn't even reach the
IsOffice2003 method, because i put a message to be displayed before the
call to the method and the message didn't appear.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
at AlcatelLucent.ALU9155.AFP.UI.BenchmarkForm.btnExport_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
ALU9155.AFP
Assembly Version: 2.1.0.50
Win32 Version: 2.1.0.50
CodeBase: file:///C:/Program%20Files/Alcatel-Lucent/9155%20b2727/Modules/A9155_AFP/ALU9155.AFP.DLL
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
Interop.AFPApiLib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.0.0
CodeBase: file:///C:/Program%20Files/Alcatel-Lucent/9155%20b2727/Modules/A9155_AFP/Interop.AFPApiLib.DLL
----------------------------------------
PropertyPages
Assembly Version: 1.0.0.0
Win32 Version:
CodeBase: file:///C:/Program%20Files/Alcatel-Lucent/9155%20b2727/Modules/A9155_AFP/PropertyPages.DLL
----------------------------------------
Interop.FSKGISLib
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Alcatel-Lucent/9155%20b2727/Modules/A9155_AFP/Interop.FSKGISLib.DLL
----------------------------------------
Interop.Atoll
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Alcatel-Lucent/9155%20b2727/Modules/A9155_AFP/Interop.Atoll.DLL
----------------------------------------
Accessibility
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Alcatel.UI
Assembly Version: 1.0.0.2
Win32 Version: 1.0.0.2
CodeBase: file:///C:/Program%20Files/Alcatel-Lucent/9155%20b2727/Modules/A9155_AFP/Alcatel.UI.DLL
----------------------------------------
ZedGraph
Assembly Version: 4.2.1.19905
Win32 Version: 4.2.1.19905
CodeBase: file:///C:/Program%20Files/Alcatel-Lucent/9155%20b2727/Modules/A9155_AFP/ZedGraph.DLL
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
The code is the following:
private bool IsOffice2003() |
{ |
RegistryKey key = Registry.CurrentUser; |
key = key.OpenSubKey(@"Software\Microsoft\Office"); |
string[] keynames = key.GetSubKeyNames(); |
for (int i = 0; i < keynames.Length; i++) |
{ |
if (keynames[i].ToString() == "11.0") |
{ |
return true; |
} |
} |
return false; |
} |
private void btnExport_Click(object sender, EventArgs e) |
{ |
if (IsOffice2003()) |
{ |
if (sFDExport.ShowDialog() == DialogResult.OK) |
{ |
Microsoft.Office.Interop.Excel.Application oXL = null; |
Microsoft.Office.Interop.Excel._Workbook oWB = null; |
Microsoft.Office.Interop.Excel.Worksheet oSheet = null; |
try |
{ |
oXL = new Microsoft.Office.Interop.Excel.ApplicationClass(); |
oXL.Visible = false; |
oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)); |
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.ActiveSheet; |
int row = oSheet.UsedRange.Count; |
|
ExportSettings(oSheet, ref row); |
|
ExportMessages(oSheet, ref row); |
|
chartUC.Export(oSheet, ref row); |
|
SaveFile(oWB); |
} |
catch (COMException ex) |
{ |
throw ex; |
} |
finally |
{ |
//close everything |
oWB.Close(null, null, null); |
oXL.Workbooks.Close(); |
oXL.Quit(); |
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL); |
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet); |
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB); |
oSheet = null; |
oWB = null; |
oXL = null; |
GC.Collect(); |
} |
} |
} |
else DisplayMessage("Export is possible only in Office 2003.", MessageType.Warning); |
} |