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.
Related
I have a Visual Studio solution that contains two projects (1) a C# console application; and (2) a C# Excel Workbook.
The console application creates some data, which I would then like to pass into the workbook.
To do this I have created a method in the Excel workbook project, which receives data from the console application. However, I cannot create an instance of the Excel book. At the moment I am trying:
Excel.Application excelApplication = new Excel.Application();
Excel.Workbook excelWorkBook = excelApplication.Workbooks.Add("MyBook.xls");
I could hard code the path to the xls file, but I don't really want to do this as I would prefer to wrap everything up into a single .exe
Any ideas?
So basically what you're asking is how to wrap two files (an exe and an excel file), into a single exe.
There is a simple (and fairly clumsy) way of doing this which revolves around using Resources in C#, meaning your program will have to do something similar to the following:
Take excel file source out of Resources, and write it to a file when the program is run (temporary file)
Use the Current Directory + excel temp file name to feed into the Worksheet.Add method
Once edited, store the temporary file's data back into the Resources of your exe.
Another possibility would be, instead of using a temporary file, read a stream of bytes directly into Worksheets.Add if this is supported by the API (feel free to tell me if this is so).
EDIT: Great tutorial on using resources in C#.
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.
I am creating a new .xls file for the Microsoft Office 2003 from existing template in C# using .NET 4.0 and Excel Interop Object Library. My template already contains all needed data and various formulas so my task is just to change some cells which are used in formulas as parameters. I do this without any problem...
The problem appears when i try to open my new file: when the file is opened at 1st time addins of excel doesnt work and as the result all the formulas show me error messages like "#NAME". When i launch the file again - everything is ok...
This makes me angry....
Does anybody know where the problem could be? And what is the solution??
Thank you in advance.
Do you open it Excel 2010? If so, then Excel thinks it's with macros and displays it incorrectly.
Basically, #Name error appears when the formula inside the cell can't be interpreted or is unknown.
The problem is related to the path of formulas, I guess in the first launch it can't correctly find the formulas path so in the next launches it shows them correctly.
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#
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