Thursday, December 14, 2006 7:15 AM
There is an existing Excel document within a label named "testLabel", and I need write a C# prgramme to open this Excel document and set the value of "testLabel" to be "MSDN Sample", How can I do that?
Now, I get the worksheet object, it's type is "Microsoft.Office.Interop.Excel.Worksheet", but how can I cover it tobe "Microsoft.Office.Tools.Excel"? And how can I get the "testLabel" and set it's text?
Thursday, December 14, 2006 12:36 PMModerator
Does this have to be a VSTO solution, or would you be doing this, for example, through a Windows Forms application?
the namespace Microsoft.Office.Tools is only valid for VSTO solutions. The value of a cell in a worksheet, opened using automation, can be set using only Microsoft.Office.Interop.
Friday, December 15, 2006 12:40 AM
My solution is a website, there's a button on the page, click the button can export some data from the page to a Excel workbook, this target workbook is create by a template workbook, there are some labels in the template, and I also need set some label's text as the data from page.
Now I use Microsoft.Office.Interop.Excel to get the Excel Application, Excel Workbook and Excel Worksheet. I know the labels are in the worksheet, but how can I get them and set it?
The way now I'm using is
((InteropExcel.Label)myWorksheet.Labels("solutionNameLbl")).Text = "Xinwen's Solution";
But it is not correct. The compiler notice me "COMException was unhandled by user code", and "Exception from HRESULT: 0x800A03EC".
Friday, December 15, 2006 1:30 AM
This forum is intended for VSTO-related questions. Please refer to the Excel newsgroup for support on the use of Excel's object model.
General programming issues: excel.programming newsgroup
Friday, December 15, 2006 1:41 AM
As Cindy points out, the namespace Microsoft.Office.Tools is only valid for VSTO solutions, and Worksheet.Labels is a method of the Microsoft.Office.Tools.Excel.Worksheet object.
Not only that, but also, as the documentation clearly states: "The Labels method supports the Visual Studio Tools for Office infrastructure and is not intended to be used directly from your code."
One way you could redesign your code to meet your requirements is to use named ranges instead. You can access named ranges through the Excel object model, without using the VSTO Tools objects. As Daniel points out, you should consult the Excel documentation to see how to work with named ranges or cell references.
For example, look at this article that describes various ways of interacting with cell ranges:
...and for further details on Excel programming, please look at the link that Daniel pointed out:
Friday, December 15, 2006 2:16 AM
Thank Andrew and Cindy so much!!!