Using ProtectContents on IWorksheetin Spreadsheetgear - c#

I've a workbook object in which I'm using excel formulas in certain cells.
I'm programmtically setting the value for ProtectContents on IWorksheet object to turn the readonly mode on those cells on and off.
1.When the value of ProtectContents is set to true, I see that all the cells are in read-only mode as expected but the cell (which use excel formula) value show up as #NAME?.Is this how it works in Spreadsheetgear?
2.And when the ProtectContents is set back to false,I see all those cells which use formula still show value as as #NAME? but they are editable as expected.But wondering why are those cells values not getting evaluated??
Please advise.
Thanks for your help.

There should not be any correlation between #NAME? errors and the state of worksheet protection. #NAME? errors can occur for a number of reasons but without more details on your particular case it's impossible to say why you are seeing this.
One common reason you might see this in SpreadsheetGear (and I presume not in Excel itself?) is because the formulas in these problematic cells either directly or indirectly depend on a User Defined Function (UDF) in the VBA portion of your workbook, from an external XLL add-in, etc. SpreadsheetGear does not support executing UDFs via VBA or XLLs, so such functions would evaluate to #NAME?.
SpreadsheetGear does support implementing your own Custom Functions in your .NET application via the SpreadsheetGear.CustomFunctions.Function class and its Evaluate(...) method. If you discover the above being the cause, you could implement your own Function to make these cells correctly calculate. I'd provide sample code but I'm going out on a limb in the first place mentioning this as being the underlying cause. Update your question with more specifics and I can update mine accordingly if needed.

Related

Excel Interop COMException HRESULT: 0x800A03EC when accessing hidden cell

I'm having issues accessing a named cell in Excel from C# using Excel Interop when the cell is hidden and I access it like so:
bool SomeBoolean = WorkSheet.Range["MyRangeName"].Value;
The Range property implicitly calls get_range when I'm accessing it, which is what's throwing the exception according to the stack trace. The code used to call get_range directly, and that had similar results.
If I unhide the cells, it works fine. If I step through the code line by line, it works fine (this is always a frustrating situation).
Of course I can solve this by unhiding the cells, but I'd like to try to find a more elegant solution. I've pored over the MSDN documentation and several threads here on SO, but I can't seem to find anything. Any help would be much appreciated!
MSDN page for Worksheet.Range: https://msdn.microsoft.com/en-us/library/office/ff836512.aspx
This is by design. By definition, hidden cells are not accessible.
Try it yourself. Create a worksheet in Excel, hide some columns, and then search (By Values, not Formulas) for a value in a hidden cell. You will get a dialog box indicating that the value could not be found.
If you don't agree with this behavior, then take it up with Microsoft.
Thanks everyone for trying to help me solve this mystery. During my debugging, I noticed that sometimes it worked, mysteriously, and other times not, and I went over the code leading up to the line in question.
The cause of this issue was that the Range is being searched from a worksheet which is set as the currently-selected worksheet at the time the plugin is called. So if the correct tab in the workbook isn't selected at call time, it fails. If it is selected, it succeeds. As it turns out it's nothing to do with the cells being hidden. Rather frustrating, but at least now we know!

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.

C# Excel.Interop Detecting a Sort Action

I have looked at the various levels(application, workbook, and worksheet) of the Excel.Interop libraries to find out how to detect a user sorting data. I have created a class that instantiates an application, workbook, and worksheet. The program has data piped in from another application and then adds it to a worksheet. I am using .NET 4.5, C# compiler version 4.0, and have been testing with Excel 2013 if that makes a difference.
I can detect cell changes and calculation events but neither of those events fire when a column is sorted. I believed that it should work after reading this article.
After having that fail for me, I looked into the table update event but I had no luck. There error I received was along the lines of, "the handler does not exist in the Microsoft.Office.Interop.Excel namespace, are you missing an assembly. I don't think this is the right direction I need to go but I was attempting to see if it would suit my needs.
Using any other method besides the Excel Interop method might set me back a few weeks.
Is there something I am missing in regards to the table update event handler or perhaps the sort event itself?

How to tell where (what cell(s)) an XmlMap is pointing to?

I need to grab the ranges (row, column etc.) a particular XmlMap in an excel worksheet is pointing to programmatically via interop. Does anyone have any thoughts? I can not see any properties/methods/members of the XmlMap interface that provide this information.
I ended up solving this problem by dumping the XmlMap output to a file, using the .NET XML library to find all of the elements in the map and the using 'Worksheet.XmlDataQuery' to find to cell/range location of each element. Its dirty, but its the only way I could get this to work.

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.

Categories