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

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.

Related

Hide sections in a word document based on users responses to a series of questions

I'm looking for the best way to achieve the following workflow.
Ask a series of questions
Captured the responses
Use the answers captured to either hide or show certain aspects of word document.
Save the complete word document to a location (TBD).
I'm not a developer, so will need to source one who could pick this up, but before I do I wanted to know the best approach to this workflow.
Appreciate any feedback you can offer.
Cheers
There are several libraries for interacting with Word (.docx) documents in C#, such as NPOI and DocX, and it is not theoretically complicated to programatically populate a document based on user input and some decision tree and then save it somewhere locally or expose it for download via a web interface. But, keep in mind, that's only part of the solution -- apps have to be hosted, secured, monitored, etc., and that's where the "hard" part is likely to be.
If you are looking to accomplish this within an enterprise environment that uses Microsoft Office 365, you may not need a developer at all. Microsoft Flow / Microsoft PowerAutomate allows you to produce complex workflows such as the one you described. There's a very similar one listed here:
https://flow.microsoft.com/en-us/galleries/public/templates/3c651e28cded46aab2ba40a2c3116f30/create-word-and-pdf-documents-from-microsoft-forms/

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.

ASP.NET and OpenXML - Create formatted/styled xlsx file - Best Solution?

This question might be more subjective, but I'm hoping someone with more experience can guide me in the right direction.
I'm brand new to web development, but have been coding C# for a couple years. My job wants me to convert an existing app we have to SharePoint 2013 and part of the app generates an excel report with custom formats and styling. In the original app we used Interop, but apparently since it's 32bit and our server is 64bit, Interop won't work. I thought about just doing a csv, but our customer is adamant about keeping the styling so I found OpenXML.
I don't have any experience with OpenXML, but I saw the tool can convert files into code. I loaded our template into the tool and it generated about 2000 lines of code which seems very excessive. Using Interop it's a fraction of the length and seems much easier to read. I'm tempted to just copy all the code over and stick it in a region (which I know most developers hate and I agree looks bad) and put a note at the top saying that if the template ever changes to just redo that region with the new one.
Is that my best option or is there a better alternative? Unfortunately our dev network is pretty closed off (it's a pain to get approval on third party non Microsoft stuff) so I'm limited on third party libraries I can add as well. If there's an option without doing that, that'd be preferred.
If you have one or more templates, just use OpenXML to create a new workbook from a template for each request. And then use code to enter values into named ranges, datasets into rows, etc.
BTW—ClosedXML makes simple and medium things a lot easier.

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.

Editing Microsoft Word Documents Programmatically

I want to know if this could be done.
I am building a data dictionary for our software system (school project), and I'm thinking of an automated way to do this. Basically I don't use much of Microsoft Word (2007), I only use it in documenting schools stuff, etc. I want to know if its possible to create/edit a Word document programmatically from a template.
The idea is, I will create a page on Word that contains an empty form that will be repeated on every page. For every data that I will input to my program, it will update the corresponding field in the form and skips to the next form.
The purpose of this, is to eliminate copy-paste methods (my habit) and to speed things up when doing the documentation.
Word automation, as suggested by others, will lead you to a world of hurt for two major reasons:
Office is not intended to be run unattended, so it can pop up message boxes at any time, and
It is (probably) not licensed to enable office functionality for computers which don't have it. If you generate a Word document on a web site using automation, you have to make sure that this functionality cannot be reached by computers which don't have office installed (unless they changed this rule in the last years).
I have used Aspose.Words, it costs a little, but it works well and is intended for this.
Not exactly sure what you really want, but creating word documents with c# shouldn't be any problem:
http://support.microsoft.com/kb/316384
If i find out your purpose correctly you need to visit this microsoft msdn link
Manipulating Word 2007 Files with OpenXML
Definitely possible. A fairly easy way of doing it using Office Automation. See this KB article for a basic sample: How to automate Microsoft Word to create a new document by using Visual C#
I think the main difference to that sample will be that you'll open your template and do SaveAs instead of creating a new document, but I can't remember exactly.
However, depending on your exact requirements, there might be better alternatives. For example, it's not recommended to do Office Automation on servers (including on webservers), so if that's needed you might want to look at something else.
You can use com interop of .net framework.
Understanding the Word Object Model from a .NET Developer's Perspective
Building COM Objects in C#
Using COM programming is not the best way as mentioned by erikkallen, I suggest using OPEN XML. It is really easy to use and your document generation operation will be very fast.
http://blog.goyello.com/2009/08/21/how-to-generate-open-xml-file-in-c-in-4-minutes/
http://msdn.microsoft.com/en-us/library/aa338205(v=office.12).aspx

Categories