I worked on VSTO C# Excel 2013. I tried and searched many approaches to find a way to trigger more events.
One of the important events I need is, cells formatted event (changing background color, merging cells.
Is there any way?
There is no such event. Your main events are for Workbook and Worksheet objects. Intellisense is presumably giving them all to you. If not, check out the MS reference for Worksheet events. There is a similar list for Workbook. These are also the same as the events available within the VBA editor inside Excel.
If you want to cheat and make an event, it involves watching all of the cells and detecting the change yourself using the Worksheet_SelectionChange event. See related: How to detect changes in cell format?
Note that approach will not work if you are trying to detect format changes that are effected by your code instead of the user (unless of course you are using Select in your code which would be inadvisable).
Related
How can one tell whether a user has changed/edited/deleted/added a formula in an Excel worksheet using C# VSTO?
So far, what I have been thinking about is to attach an event handler which can tell me whether the user is changing a cell. But even this approach is problematic because existing events relating to user editing cells do not seem to be able to tell you the previous and the new value, so I do not really know if an old formula is being overwritten. What is the right approach to this question?
How about you handle SheetSelectionChange, cache the selection values and then when (and if) SheetChange is raised you compare the new value(s) with the old.
We have a couple of spreadsheets here that we populate with some data and do some formatting using Microsoft.Office.Interop.Excel.
Now that part is all good - it's something along the lines of what is done here: Write to Excel example.
What I'd like to do is:
Add a button inside the spreadsheet, bound to a macro.
When the user clicks the button, the macro fires - we grab some parameters from the cells and pass those to the C# function. That code does all the heavy lifting, populates the spreadsheet as above, etc.
What I don't understand how to do is that second step - I can't see a way to actually pass parameters and call into C# directly.
As it stands now, I can call a C# console application and pass command line parameters into it... but that seems a bit wrong, I'd have thought there would be some better way - but I can't seem to find it!
Coopernick,
It looks like you need to develop an Excel add-in and move the VBA code on the add-in rails. See Walkthrough: Creating Your First Application-Level Add-in for Excel to get started quickly. You may consider an Excel add-in as a regular .Net application where you can use any components and libraries.
You may find the Excel Solutions section in MSDN helpful.
Is there a way to know if a column has been clicked in Excel? I'm using VSTO and .NET 4. I can't find any events for this kind of situation.
It sounds like you're thinking of Worksheet.SelectionChange.
See also: How to get the selected range.
I'm working on converting an old VBA project into a C# VSTO. I know a lot of the code has to go out of the window, but I was hoping to still use the layout and objects that had been embeded into the sheet. Examples of the objects include controlbuttons, labels, images, etc.
All of these persist nicely when I choose the document as a template when making a new project. I run into the problem in that I can't seem to select or reference any of these objects. For example there's a button in the middle of a sheet. If I open the sheet in VS2012, I can see the button. I can even "highlight" it but not select it. If I right-click on the button to pull up the properties, it will bring up the properties for the sheet, not the button. I can't resize them or move them around (not that I want to).
I also can't seem to reference them in code, ie ...Sheet1.ControlButton1... or ...Sheet1.OleObject(n)... will never refer to anything. If I open up the workbook directly from the solution folder and click on the same control I can see properties, and the formula bar in Excel reads =EMBED("Forms.CommandButton.1","").
I've also not been able to loop through all the OleObjects and just display their names. Clearly where ever the collection is that is storing these I can't find it.
Try the Workbook.Shapes collection
Edited as per the comments below. This works:
Globals.ThisWorkbook.Worksheets(1).OLEObjects("CommandButton1");
I have a row in excel I'd like to translate into an ObserveableCollection in C# for binding/event purposes, so all accessor classes know they're getting the latest data from the source excel sheet. How would this be done?
Clarification: I'm using an excel add-in project, not a workbook project, so am not sure whether or not XMLMappedRange Controls are an option.
Using VSTO you have a few options:
From the Excel.Worksheet class, you can access the Worksheet.Change event.
From the NamedRange class, you can access the NamedRange.Change event (which uses the Microsoft.Office.Interop.Excel.DocEvents_ChangeEventHandler delegate that you mentioned in another comment).
The NamedRange class also supports simple, one-way databinding via the DataBindings property, an example of which is shown in the discussion How do I bind an array to a NamedRange.
Another possibility is the XmlMappedRange control, which also supports databinding.
A good primer on using the NamedRange and XmlMappedRange can be found here: The VSTO Programming Model. A decent walkthrough using the NamedRange can be found in the Visual Studio Tools for Office (VSTO) 2005 Guided Tour.
I hope this helps...
Mike
You can add OnChanged in your range changed event.