Discussion:
Update Active Cell
(too old to reply)
AD
2007-06-25 14:05:55 UTC
Permalink
Hi,

I just started with VSTO and I'm creating an Excel Addin.
I have heard so many great things about VSTO, but at the moment I'm
wondering about just how great it actually is.

I'm trying to write a value to the Active Cell, and expected the
following to work:
Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.ActiveCell.Value
= "asd";

Quite simple, only problem is that it doesn't work... What is the best
way to write values to the active sheet? I have tried so many things
with no success...
Harry Miller [MSFT]
2007-06-25 16:10:40 UTC
Permalink
You're into the Excel object model there--pretty wild territory. I asked the
writer for the VSTO 2005 SE documentation about your question, and here's
his reply:

The following simpler code works:

Globals.ThisAddIn.Application.ActiveCell.Value2 = "asd";

Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet doesn't have an
ActiveCell property. (As an aside, you have to cast most objects to their
specific type to use them. For example, if ActiveSheet DID have an
ActiveCell property, you'd have to cast ActiveSheet to a
Microsoft.Office.Interop.Excel.Worksheet before you could use it this way,
because ActiveSheet is defined as an Object in the primary interop assembly,
and you need to cast it to the right type.)

If you just want to get or set the value in the cell, use the "Value2"
property instead of "Value", because "Value" is a parameterized property (so
you have to use get_Value or set_Value methods in C#. There's information
about that in Eric Carter's blog at
http://blogs.msdn.com/eric_carter/archive/2004/09/06/225989.aspx).
--
Harry Miller
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by AD
Hi,
I just started with VSTO and I'm creating an Excel Addin.
I have heard so many great things about VSTO, but at the moment I'm
wondering about just how great it actually is.
I'm trying to write a value to the Active Cell, and expected the
Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.ActiveCell.Value
= "asd";
Quite simple, only problem is that it doesn't work... What is the best
way to write values to the active sheet? I have tried so many things
with no success...
AD
2007-06-26 05:28:05 UTC
Permalink
Hi Harry,

Thanks for the post, looks quite interesting. Never thought C# and VB
would be that far apart... I was actually hoping that the latest VSTO
would be more aligned with the .Net than with Office (which I thought
was the purpose of VSTO :))

I've also found ((Range)Globals.ThisAddIn.Application.Cells[x,
y]).Value2 to work quite well as I am looping, do you know which way
is the best to copy a DataSet to a worksheet? At the moment I'm
looping through every column in every row, which I do not like...
McLean Schofield [MSFT]
2007-06-26 16:42:02 UTC
Permalink
If you want to copy a set of data from a DataSet to cells in a worksheet, the
best way to do this is to copy the data to an array, and then to copy the
array to the Value2 property of a range that has the same size as the array.
This is much faster than looping through the data and setting cell values
individually. Eric Carter blogs about this too:
http://blogs.msdn.com/eric_carter/archive/2005/03/16/396886.aspx

The following code demonstrates a simple example. The code creates a simple
DataSet, copies the contents of the DataTable in the DataSet to a
two-dimensional array (the example does this by looping through the DataTable
- there might be an easier way to do this, as I'm not an expert in this
area), and then assigns the values in the array to a range in one simple
operation. This code just copies the data to a range beginning at cell "A1",
but you can of course specify any range in the workbook, as long as the range
has the same size as the array.

I hope this helps!

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
#region VSTO generated code

this.Application =
(Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), this.Application);

#endregion

// Get the sample DataSet. For simplicity, assume we're just
using the first DataTable.
DataSet ds = CreateSampleDataSet();
DataTable dt = ds.Tables[0];

int numberOfRows = dt.Rows.Count;
int numberOfColumns = dt.Columns.Count;

object[,] dataArray = new object[numberOfRows, numberOfColumns];

for (int i = 0; i < numberOfRows; i++)
{
for (int j = 0; j < numberOfColumns; j++)
{
dataArray[i, j] = dt.Rows[i][j];
}
}

// Import the data into a range with the same size as the array.
Excel.Range theRange =
((Excel.Worksheet)this.Application.ActiveSheet).get_Range("A1", "B2");
theRange.Value2 = dataArray;
}

private DataSet CreateSampleDataSet()
{
// Create and populate a simple DataSet with 2 rows.
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Customers");
dt.Columns.Add(new DataColumn("LastName"));
dt.Columns.Add(new DataColumn("FirstName"));

DataRow dr = dt.NewRow();
dr["LastName"] = "Chan";
dr["FirstName"] = "Gareth";
dt.Rows.Add(dr);

DataRow dr2 = dt.NewRow();
dr2["LastName"] = "Applewood";
dr2["FirstName"] = "Gary";
dt.Rows.Add(dr2);

return ds;
}
Post by AD
Hi Harry,
Thanks for the post, looks quite interesting. Never thought C# and VB
would be that far apart... I was actually hoping that the latest VSTO
would be more aligned with the .Net than with Office (which I thought
was the purpose of VSTO :))
I've also found ((Range)Globals.ThisAddIn.Application.Cells[x,
y]).Value2 to work quite well as I am looping, do you know which way
is the best to copy a DataSet to a worksheet? At the moment I'm
looping through every column in every row, which I do not like...
AD
2007-06-27 05:49:26 UTC
Permalink
Hi McLean,

Thanks for the post. I am using something to that effect, and I can
probably finre tune it to be similar to what you have...

foreach (DataRow Row in TableData.Rows)
{
Range asd = ExcelApp.get_Range(ExcelApp.Cells[x, y],
ExcelApp.Cells[CurrentRow, x + TableData.Columns.Count - 1]);
asd.set_Value(null, Row.ItemArray);

CurrentRow++;
}

I was hoping I could databind the sheet to the DataSet, I suppose I
expecting too much from VSTO...
Harry Miller [MSFT]
2007-06-27 16:01:09 UTC
Permalink
You can do something very similar to what you want by using VSTO 2005, but
not with an add-in and VSTO 2005 SE. You'd have to create a document-level
customization instead of an add-in, and the free downloadable Second Edition
doesn't have document-level customizations--you'd need to buy the full
version of Visual Studio 2005 Tools for Office (which is a full edition of
Visual Studio 2005: http://msdn2.microsoft.com/en-us/vstudio/aa718673.aspx).

If you have the full VSTO 2005 with document-level project templates, you
can just drag a ListObject control from the Data Sources window to the
worksheet and it will automatically bind to and display the target data, and
you can have two-way interaction between the worksheet and the data source.

Here are a couple of articles about that:

Walkthrough: Binding Cells to Multiple Columns in a Table
http://msdn2.microsoft.com/en-us/library/ms178806(VS.80).aspx

ListObject Control
http://msdn2.microsoft.com/en-us/library/2ttzcbhb(VS.80).aspx
--
Harry Miller
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by AD
Hi McLean,
Thanks for the post. I am using something to that effect, and I can
probably finre tune it to be similar to what you have...
foreach (DataRow Row in TableData.Rows)
{
Range asd = ExcelApp.get_Range(ExcelApp.Cells[x, y],
ExcelApp.Cells[CurrentRow, x + TableData.Columns.Count - 1]);
asd.set_Value(null, Row.ItemArray);
CurrentRow++;
}
I was hoping I could databind the sheet to the DataSet, I suppose I
expecting too much from VSTO...
Loading...