Excel and C#.NET - c#

I'm currently developing a C # application which currently uses a VBA macro in an Excel perform data extractions.
The Excel file is essential to achieve the extraction of data and this file will be located on the customer's computer. As this code is private content and has an intellectual property law, I wanted to know how I can deploy to my customers VBA code while protecting the code?
I heard about VSTO, is it possible to run VBA code from a VSTO plugin? It is correct security level?
Thank you

If your going to create a VSTO plug in then why is the VBA needed? You can just manipulate the data directly from your C# code as per the following link:
https://msdn.microsoft.com/en-us/library/cc668205.aspx
If the point of the application is to export data from the database, why not use a library such as EPPlus and just export the code from an external application? Again, no VBA code for someone to potentially see.
Either way, your code is not in the public domain of a standard user. If your really concerned about your code being high jacked, it is worth reading up on Obfuscation too

I wanted to know how I can deploy to my customers VBA code while
protecting the code?
Excel provides a mechanism to lock access to the macro source code any workbook which I assume you know about. E.g. see here.
I heard about VSTO, is it possible to run VBA code from a VSTO plugin?
Yes
It is correct security level?
The VBA security has nothing to do with how you call it. If a macro is protected than it will stay protected no matter how you call it. To say it another way, calling a macro from VSTO does not protect it in any way.
If you want to "hide" a function from the user simply protect the project and mark the function as private. But even if the function is public the user can't see the source.

Related

Call Method of Excel COM Addin from C#

My Excel can download data from a data provider via an AddIn.
From the help pages of the provider, I found out (and tested) that it is possible to "Refresh" the data by calling the following command in VBA:
Application.COMAddIns("PowerlinkCOMAddIn.COMAddIn").Object.RefreshWorkbook
After spending a couple of hours searching on the internet I couldn't find an answer on how I can call exactly the same method by using C#.
As I have many of these spreadsheets that need to be updated regularly, I am trying to automate the process.
One way to probably achieve this would be to inject a new macro every time I open the spreadsheets with the above command and call it, but I would prefer calling it directly with C# if possible.
A second solution would be to use Python or another language to
download the data in CSV format and link my spreadsheets to the CSVs
but this process also seems an overkill.
Many thanks in advance.
You can use the late-biding technology (Reflection in .net) for calling methods and properties without casting an object to the actual type, see Type.InvokeMember for more information.
Application.COMAddIns("PowerlinkCOMAddIn.COMAddIn").Object.GetType().InvokeMember("RefreshWorkbook" ...
Also you may find the HowTo: Communicate with a COM add-in from a standalone application article helpful. A sample code in C# is included.

How to add plugin for Excel Online?

I would like to use Excel functionality from a browser. I currently have an excel based Microsoft office application. It adds an icon to the ribbon and does C# based custom application logic to communicate to a database server.
Since it requires application releases for any changes in the schema I am curious if I can convert this into an web application and still benefit from Excel's built in functionality.
I am expecting this to be possible since Microsoft Excel Online is run inside a browser. My question is, is it possible to add plugin to such online excel document? If yes, any example would be much appreciated.
You absolutely can, and depending on approach you can do so with nothing more than a VSTO project template. This is little more than a ClickOnce application. This can launch a browser within a window to perform the logic.
You may use the Office Interop assemblies to decouple your application from Office apps, yet still communicate easily. Make sure you set Embed Interop Assemblies to true once added as a reference.
For those using Office in the browser, it seems as though Microsoft is shifting into CSOM/JSON/REST, meaning this solution is coded entirely differently than COM. This should help you get started:
https://msdn.microsoft.com/EN-US/library/office/dn268594.aspx.

Convert VBA in access to C#

I have acces file with forms that create excel file with multiple spreadsheets and I have a task that converts it to C# application that do the same thing the VBA code do and it has alot of code
Can I convert the VBA code to (e.g .dll file) that enable me to use the VBA methods in C# or anyway to use VBA code in C# instead of convert it ?
Thanks,
Mohammed Thabet Zaky
http://thabettech.blogspot.com/
If you want to do it yourself, it is possible to just reference the COM stuff. I did this with another application that utilized VBA (AutoCAD). What we ended up doing was creating a .net class that referenced the COM interop and then copied all of the code into that project. This is how we created the .dll file. Then we slowly refactored the vba code into C#.
The copy and paste isn't completely seemless. You do need to make some adjustments. For example thisworkbook, activesheet, and all of those items that are referenced automatically don't work automatically (because you are not out of Excel's process). But (at least for us) it was better than completely re-creating the logic.
Here is a link to some reference material - http://au.autodesk.com/?nd=e_class&session_id=5084. It references Autocad, but doing it in Excel would be very similar.
Hope this helps.
You may have success with the VB Migration Partner, even though it was designed for Visual Basic applications, not office. At least in terms of syntactic migration from VBA to C# it could help.

How to copy from C# control and paste link into excel

I have an application that I want to link to excel. I have no preference which control is used as long as I can copy the data or control, and paste link into excel. When the data changes in my application, I want the cell to change in excel.
I have a client that claims it is possible and he has seen it, but has no proof and may be confused.
I have searched the internet and have come up with a number of half-solutions, and people who want the opposite of what I want. Does anyone know the full solution?
I think you're looking for the IDataObject COM interface.
If you expose your data as an OLE object, you can use IDataObject::DAdvise to notify Excel when the data in your application is changing.
This takes some effort, as you have to implement the IDataObject interface (along with a few others) to allow Excel to properly display and host the information you are exposing.
If this is what you're after, here are some links you may find helpful:
http://winapi.freetechsecrets.com/ole/OLEIDataObjectDAdvise.htm
http://www.catch22.net/tuts/dragdrop/1 (mostly deals with Drag/Drop)
http://www.catch22.net/tuts/dragdrop/2 (mostly deals with Drag/Drop)
http://www.catch22.net/tuts/dragdrop/3 (mostly deals with Drag/Drop)
http://us.generation-nt.com/answer/support-iadvisesink-word-excel-powerpoint-help-26654252.html
http://www.tenouk.com/visualcplusmfc/mfcsupp/ioleobject.html
Another option would be to develop an Excel plug-in using VSTO. The difficult part of this architecture would be that you have to implement your own IPC between the source application and the plug in. However, I'd choose this over IDataObject (DDE Emulation) based on prior experience with DDE.
The advantage of the IDataObject approach is that a typical old school Excel power user will be comfortable with it; and it doesn't require any plug-ins to function. If the act of dragging the control and dropping onto Excel is an important part of the scenario, then it's probably worth implementing a COM server. However, if your client isn't familiar with DDE and just wants to have Excel updated dynamically, that's probably not worth the effort.

.NET: If my .NET automates Office, does the customer have to have Office installed?

If i create a program, which in one small out of the way area, uses Excel automation:
will the application fail when Excel is needed
or will the application fail to start?
Update
Let me ask the same question, but in a more drawn out way:
Will the application be usable by
99.9% of the users who never use the feature that requires Excel
0% of the users, since Excel is not installed.
Let me ask the same question another way:
Will an application fail to initialize that references the COM interop dll's?
Let me ask the same question another way:
Will an application that doesn't use Excel, but references the COM interop DLL's fail to start?
Let me ask the same question another way:
Will an application that doesn't use Excel be usable if Excel is not installed, if that application has a dependancy on the Office Primary Interop dlls?
Let me ask the same question another way:
If my application doesn't use Excel, does the user have to have Excel installed?
The code will properly execute until it tries to make a call to the automation libraries, at that time it will generate an exception.
I have an app that uses Excel automation and I can definitively say that it will fail at runtime, not at load time. In fact we check to see if it's even installed and only show the "Show data in Excel" button if we find it (but the PIAs are deployed to all installs).
If you automate Office... you gotta have Office!
Having said that, the answer changes if you twist the question into: Can I create an Excel spreadsheet without having Excel on the client?
There are many third-party controls to do this; you can write directly to the MS SpreadML XML spec (http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx; or use third-party libraries that do (like http://www.carlosag.net/Tools/ExcelXmlWriter/).
I think it will fail at runtime, because the Interop Assemblies should be able to load without the COM-Components available on the clients system.
UPDATE: Runtime should mean when you need it!
Using COM in Win32, it will fail when needed.

Categories