views:

186

answers:

1

In the following, I am trying to persist a set of objects in an excel worksheet. Each time the function is called to store a value, it should allocate the next cell of the A column to store that object.

However, an exception is thrown by the Interop library on the first call to get_Range(). (right after the catch block)

Does anyone know what I am doing wrong?

private void AddName(string name, object value)
        {
            Excel.Worksheet jresheet;
            try
            {
                jresheet = (Excel.Worksheet)_app.ActiveWorkbook.Sheets["jreTemplates"];
            }
            catch
            {
                jresheet = (Excel.Worksheet)_app.ActiveWorkbook.Sheets.Add(Type.Missing, Type.Missing, 
                                                                       Type.Missing, Type.Missing);
                jresheet.Visible = Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetVeryHidden;
                jresheet.Name = "jreTemplates";
                jresheet.Names.Add("next", "A1", true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }
            Excel.Range cell = jresheet.get_Range("next", Type.Missing);
            cell.Value2 = value;
            string address = ((Excel.Name)cell.Name).Name;
            _app.ActiveWorkbook.Names.Add(name, address, false,
                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                                      Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            cell = cell.get_Offset(1, 0);
            jresheet.Names.Add("next", ((Excel.Name)cell.Name).Name, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }

Since the exception is thrown from a COM library, the embedded information does not seem very useful. However, here it is:

"Exception from HRESULT: 0x800A03EC"

"\r\nServer stack trace: \r\n\r\n\r\nException rethrown at [0]: \r\n at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n at Microsoft.Office.Interop.Excel._Worksheet.get_Range(Object Cell1, Object Cell2)\r\n at WorkbookTemplateManager.EditTemplateForm.AddName(String name, Object value) in C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs:line 143\r\n at WorkbookTemplateManager.EditTemplateForm.SaveTemplate(Object sender, EventArgs args) in C:\Documents and Settings\QueBITuser\My Documents\Visual Studio 2008\Projects\JRE.WCF\WorkbookTemplateManager\EditTemplateForm.cs:line 124\r\n at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n at System.Windows.Forms.Button.WndProc(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)"

A: 

From some looking around, I found some examples where the string itself has an equals and an explicit reference on the Names.Add method. Something like:

 jresheet.Names.Add("next", "=jreTemplates!$A$1",...
Richard Morgan