I have a problem about how to define Excel macros via C# code.
For example, I create a button in Excel, and then assign a C# function to it as its macro function instead of using VBA. Can anybody give me some point?? Thanks..
Here is an Article that explains what you are trying to do I hope this helps
Excel Macro -Create and Run at Runtime using C#
Related
is there a way to use C# in Excel? I mean using C# to automatize excel spreadsheet doing what it is already possible with the VBA editor. I usually use excel for my job tasks but the only programming language I know is c# and I don't want to start with VBA now.
Try this library :ClosedXML
You can almost do everything on that library, and you don't need to have office installed.
We have a .Net/C# based web service.
Within the service, we use a spreadsheet as a calculationr engine.
We're using Apose.Cells for .Net to populate the spreadsheet cells.
Calculations are performed through VB script embedded in the spreadsheet.
To be able to generate results, I need to be able to run/trigger the VB script.
Aspose does not seem to support this functionality.
It can poke/retrieve data and force recalc of any formulae that exist in the spreadsheet, but not run macros/VB script.
Once I've uploaded the desired data into the spreadsheet, how do I run the macros through .Net?
Thanks,
JohnB
Aspose.Cells cannot run macros. Besides, we are afraid, there is no server-side solution for your problem too. However on client-side (local machine) you can execute macros using Microsoft Office APIs. Please use the namespace
i.e
using Excel = Microsoft.Office.Interop.Excel;
Here is an example how to make use of it.
https://social.msdn.microsoft.com/Forums/Lync/en-US/2e33b8e5-c9fd-42a1-8d67-3d61d2cedc1c/how-to-call-excel-macros-programmatically-in-c?forum=exceldev
Note: I am working as Developer Evangelist at Aspose
I have an excel file that contains some methods written in VB I think.
I want to know any way makes me use these methods in C# code.
I tried to make a connection with the file using (System.Data.Odbc.OdbcConnection), and then use the UPDATE statement by using Command, but error toke place.
So does any one know how to do this?
I uploaded the file on :
Download The Excel File
I want to change C20 and C21, then get the data from C22:C29
thanks
What you are looking at is likely not VB.NET, but VBA (Visual Basic for Applications). VBA is an ancestor of VB.NET, and the language that is used for Excel macros. The bad news for you is, VBA isn't a .NET language, and you can't use C# for macros "attached" to a workbook.
If you are really motivated to use C#, your best option is to look into ExcelDNA, which among other things allows you to call .NET dlls from Excel.
However, I would really question the need to convert to C#, given the problem you describe. VBA is the "natural" language for lightweight Excel automation, and is fairly understandable. If you have existing code, just modify it. And one of the nice features of Excel VBA is the macro recorder, which allows you to record your interactions with Excel as VBA code, which is a great way to figure out how things are used in the object model.
Is it possible to write a WPF C# application that can load and read a VBA Macro from a separate text file, open an existing Excel workbook, and process the VBA macro against it?
I am NOT trying to convert VBA to C# or generate VBA from C#. The application, VBA Macro text file (the VBA is extracted and saved separately on a text file), and Excel are all written / generated separately by different people.
The application load and read a text file that contains VBA, and open an existing excel file and run the Macro against it either by injecting the macro to excel or even better, leave the workbook without a macro in it.
I have no idea how or where to start, or if that is even possible. The closet I found on SO is Injecting vba macro code into excel using .net but doesn't seem to help. I'm looking for any suggestion to get me start and I'll share and update my progress here.
Here is the way to add some VBA from file in VBA (shouldn't be so hard to adapt it to C# but I am not familiar enough to C# syntax):
Function Insert_VBACode(ByRef oWB As Workbook)
Dim oVBP As VBProject ' VB Project Object
Dim oVBC As VBComponent ' VB Component Object
On Error GoTo Err_VBP
Set oVBP = oWB.VBProject
Set oVBC = oVBP.VBComponents.Add(vbext_ct_StdModule)
oVBC.CodeModule.AddFromFile "c:\VBADUD\Templates\MSample.bas"
oWB.Application.Run "'" & oWB.name & "'!SayHello"
oWB.Save
End Function
[Source] (have a look there if you want to add any error handling I've removed here for more readability).
You can also open the workbook instead of taking it as an argument of the function.
And then, you can call your imported macro from C# thanks to this walkthrough from MSDN.
I have a program that reads Data from Excel and write data to Excel file. I need to send commands from the Excel file to the c# program (e.g. capture the F9 from the excel and get it on the c# program)
I am using Microsoft.Office.Interop.Excel framework.
How can i do it?>
the excel application interface also provides events to some excel functions, e.g.
Microsoft.Office.Interop.Excel.Application:
//
// Summary:
// Occurs after any worksheet is recalculated or after any changed data is plotted
// on a chart.
event AppEvents_SheetCalculateEventHandler SheetCalculate;
You will probably have to call native Win32 methods to add a message hook looking for a character message in the Excel window.
oferyo,
Hello. You don't mention what sorts of volume of data you need to deal with? Or the type, either (questions like: does the stuff 'sent' to the destination Excel file includes graphs or just data?).
You don't mention which version of C# you are using but you could consider calling a web service from the VBA script embedded in or added to the originating Excel spreadsheet?
MSDN article on calling WebServices from within VBA
I can't recall how to plumb VBA code into specific events on the Excel spreadsheet (like the recalculate/press of F9) but I think that should be trivial.
If you are using C# 3.5 or later you can host a WCF service within the application ... but I would suggest describing the question better because it may be the VBA->WCF/WebService->Application solution might be seriously over-engineered?
Hope this helps you think about the options you have within VBA ...
regards,
Aidanapword