Call Method of Excel COM Addin from C# - 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.

Related

SAP GUI Automation using C# - does sapfewse.ocx have a written developer documentation or api library?

Referring to this question here a past user was able to develop some C# code to perform SAP GUI Automation. However when looking online at tutorials/developer documentation for this library I was unable to find anything.
There are a lot of objects like like, GuiSession, GuiApplication, GuiConnection that I can't find information to online.
How would I go on to writing SAP GUI automation using C# without proper documentation?
There is very little documentation for SAP Scripting in general outside of the one resource posted in the linked question (http://www.synactive.com/download/sap%20gui%20scripting/sap%20gui%20scripting%20api.pdf).
However, I also found this resource (http://help.innowera.net/PR2008/2.00/SAP_Scripting_API.pdf) which is similar to the one above but I think has a bit more detail.
Further, there is a built-in help document within SAP that you can access from the "Customize Local Layout" Menu (click the Monitor Icon in the second-from-the-top toolbar or ALT+F12) and then selecting "SAP GUI Scripting Help"
All three of these resources are primarily for VB; however, the objects, methods and rules are nearly the exact same for C#.
That being said, I do not know of any API Documentation specific to C# at this time
How would I go on to writing SAP GUI automation using C# without proper documentation?
I suggest recording scripts in VB through SAP first so that you can at least get the IDs for each button, menu, and field you use for a particular report. Then you can copy the IDs from that script into your C#. Additionally, these VB scripts can help you determine which objects and methods to use in your C#.
You can access the script recording functionality through the same "Customize Local Layout" menu and selecting "Script Recording and Playback..."
This functionality records every mouse click and keyboard input inside of SAP in VB and stores the resulting script in %AppData%\SAP\SAP GUI\Scripts
As I find more resources for SAP Scripting with C#, I will post them here.
There's now this HTML documentation online:
SAP GUI Scripting API
GuiSession Object
GuiApplication Object
GuiConnection Object
Etc.
The documentation is in your Program Files > SAP. Then, search for this file "SAPGUIScripting.chm". This is the best documentation I've seen. It documents all class objects with their methods, properties and events. It will occasionally provide some direction on how to use some of the methods.
You can also click on the "SAP GUI Scripting Help" as seen below.
As far as using C#, you would use the methods and properties just like you would for any other C# object.
Best of luck and happy scripting!

Excel and C#.NET

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.

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