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.
Related
Hi Stackoverflow Users
I was recently assigned to rewrite a excel document my company uses a lot. Originally it was written in matlab, but to easier distribute it, they migrated it to excel. It was done very bad, and therefore it worked on some computers and not on others. So it was rewritten to vba. But now they want it to be able to do very complex calculations like Monte Carlo simulations. therefore we wish to write it in c#.net for performance and development reasons. the last week i have tried and tried to learn how it was best done. I found several possible solutions, but i have not found a single way to do it which meets my 2 simple demands:
The user doesn't have to go to any settings and add something, if it have to be done, it need to be done in code
The calculations is done at the press of a button, so i need to be able to receive the event
I have spent so much time on the web, but i don't know exactly what to search for, so it all just seems like a big wall of unexplained, complex com-magic.
So what are your suggestion? Is there any solution which meet those demands? Any suggestions, references or any other help will be deeply appreciated.
Excel doesn't natively support C# in macros, so you're not going to be able to meet your first demand in a stand-alone spreadsheet. If you're willing to install an add-on into Excel, you might want to consider Excel-DNA. This would allow you to compile a .NET DLL containing the functions you want to add in to Excel.
Receiving an event from a button click is simply a case of adding a button control onto a sheet, and assigning a macro to it. This macro (e.g. a VBA function) will be run on demand when you click the button.
I have an excel workbook which needs to query data from an HTTP server. Query directly from a remote database isn't an option.
From what I know I can use VB macros to contact the HTTP server and update the cells in the workbook. Is it possible to do this in C#? I don't want to additional burden of deploying an extra application.
Thanks for your help.
You can use the VSTO to create a document or application level addin to Excel. Look here http://msdn.microsoft.com/en-us/office/hh128771.aspx
Follow the first couple of repro steps here to create a Visual Studio Tools for Office - Excel Add-In.
Skip the last step about the calender control.
In the Excel AddIn Project > add a Ribbon Bar and Button and you can
invoke methods to collect the data via http and refresh the workbook.
Everything in VSTO is pretty much the exact same way you would do it
in VBA but in C# syntax.
Extend Your VBA Code With VSTO
When it comes to deploying you should use a Setup Project and it will create an MSI.
If you have a App.Config file be aware when deploying you need to set the setup project to add a registry key: https://stackoverflow.com/a/10525693/495455
I highly recommend this blog for tips on better VSTO memory management via .Net code:
http://jake.ginnivan.net/
I'm trying to create create a simple word template on the fly which would just contain text and a few text input fields and then open a new word document based on that template for the user to fill out. I don't think this would be too difficult using Word automation and C#, but I've never worked with Word's COM interface before.
Any links to examples or fresh examples on how to create a simple template with some text and a text input field and then how to open a document based on that template in C# would be greatly appreciated.
Thanks in advance!
I think "Office Interop" is the keyword you are looking for.
You'll find a starting point here: http://support.microsoft.com/kb/316384
Several drawbacks involved (for instance, installed Office on client machine required; not advisable to be run as a server process), but might do the job for your requirement.
I have done a fair amount of work with Word automation - though not really so much with tempates... My advice would be first of all to have a look at the documentation that ships with the product - which is a simple matter of editing a word macro, and pushing F1 in the code editor. I have found that to be a rich source of information...
The other piece of advice I would give you is to try recording a macro in Word that covers any task you want to do, and then inspecting the code that is generated. This has proven invaluable to me in getting stuff up and running quickly...
Hope this helps...
Martin.
Every week I produce a word document with some copy, tables and charts from various sources on our network.
It can take a bit of time and sometimes I make mistakes or forget stuff that should go in. I want to automate this process as much as possible.
So basically I want to make a Word Template or Console App that when I open/run it it goes off and collects all this stuff and links it into the various parts of the document.
Assume I have to insert:
Some copy from another Word document
A PNG (pie chart)
Create a table from a CSV file
Have a standard Header and Footer with page numbering
I usually make a few changes to the copy in the document to highlight the highlight from the week.
Then I check it into a SharePoint where network users can open and view it.
I figure the thing to do is crack open VS2010 and make a Word Template project. I have never done this before and I wondered what traps there are or if its even an appropriate way to solve my problem.
My other option would be some power-shell but i'm no expert there either.
I'd be pretty comfortable writing console apps so I might end up doing it that way.
Help/Advice appreciated.
I would approach this problem by breaking down the individual steps as you would perform them if you were sitting in front of a blank Word document. Then automate it using the object model. There's a little bit of a learning curve, but once you get past that you'll be addicted to automating Office. Office is quite a powerful platform. Most of what you can achieve via the User Interface can be done programatically. I do quite a bit of automation with Excel. The code is fairly intuitive...its things like worksheet.Range["A1"] = "abcd" (setting cell A1 = "abcd").
here are some pointers:
http://support.microsoft.com/kb/316383
http://msdn.microsoft.com/en-us/library/ee861527.aspx
If you go through the tutorial in the first link, you'll get the gist of it. What's great is that you can use the debugger to step through your lines of code. As each line executes, you can see the results on the Word document. I've never used a Word Template project, so I can't speak for the pros/cons there. Going the object model route, you just have to articulate what you want to have happen, break it down into the individual steps as you would perform them, and then code it up. If you get stuck, likely someone else has blogged/posted about how to make something happen. Google will find a solution very quick. Good luck!
You have lots of options:
automate Word
do it as a macro (dotm)
create your document using Open XML SDK
VSTO
You are probably best off doing it using a macro first, though this is old VB6. The techniques you quickly learn there translate pretty well into the other approaches. Open XML SDK has the virtue of not needing Word to create the document.
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.