Question : Excel interop

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:


   


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.


Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
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);
        }

Answer : Excel interop

- Are you using the primary interop assembly?
- Is the primary interop assembly deployed on the target machine?

First: I have no information about the "specific version" not being displayed. My appologies for that.

Secondly: a better test for the version is attached as snippet. Usage: GetOfficeApplicationVersion("Excel.Application"), "Excel.Application" is the version independent progId (for word: "Word.Application").

Thirdly: you are absolutely right - it's not upon loading the type, but upon accessing the function. I remembered that one wrong. And retested it.

Fourthly: Charts - grin - just try that safe to xml once ... you'll see the result ... it might surprise you. Even a word "xml" with an embedded excel "xml" part in it can be created... (and vice versa). Images can be included, etc...

Kind regards.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
        public enum OfficeVersion
        {
            Office_Unknown = 0, // error
            Office_95 = 6,
            Office_97 = 8,
            Office_2000 = 9,
            Office_XP = 10,   
            Office_2003 = 11,
            Office_2007 = 12
        };
 
        [System.Security.Permissions.RegistryPermission(System.Security.Permissions.SecurityAction.Demand, Read = "HKEY_CLASSES_ROOT")]
        public static OfficeVersion GetOfficeApplicationVersion(string progID)
        {
            RegistryKey rootKey = Registry.ClassesRoot.OpenSubKey(progID);
            if (rootKey == null)
                return OfficeVersion.Office_Unknown;
 
            RegistryKey curVerKey = rootKey.OpenSubKey("CurVer");
            if (curVerKey == null)
            {
                rootKey.Close();
                return OfficeVersion.Office_Unknown;
            }
 
            // Get the Version information
            string progAndVersion = (string)curVerKey.GetValue("");
 
            // Close the registry keys
            curVerKey.Close();
            rootKey.Close();
 
            // Error while querying for value
            if (progAndVersion == null)
                return OfficeVersion.Office_Unknown;
 
            int lastDot = progAndVersion.LastIndexOf('.');
            if (lastDot < 0)
                return OfficeVersion.Office_Unknown;
 
            string version = progAndVersion.Substring(lastDot + 1);
            return (OfficeVersion) int.Parse(version);
        }
Random Solutions  
 
programming4us programming4us