Force an Excel file to be associated with a VSTO addin - c#

This is my use case:
I need to develop a excel VSTO add-in that prevents a user from accessing a excel file unless the add-in is installed. This add-in basically authenticates the current user, and if that is successful, it will grant them access to the file. Basically I need a way to protect a file that uses my own authentication (and not the built-in password protection provided by Microsoft. Why? Because I need to be able to revoke access to the file and a local password would prevent me from doing so)
I know how to write the add-in, what I don't know is, how do I force a file to be associated with that plugin? I tried using a custom property, but that is very hack-able. Basically, I would have a custom property that says: "use this add-in", but a knowledgeable user could just go in Advanced Properties and simply delete it.

My suggestion would be for you to use COM Add-ins, as they do not distribute the code to the user and can't be easily hacked. A simple Add-in, that uses .XLA or .XLAM files are simply password protected VBA projects that can be hacked.
Now, for your workbook, I suggest you encrypt and scramble all the data and use your COM Add-in to decrypt your data and use a function to prevent saving the workbook un-encrypted. You could just override the Workbook_OnSave method to encrypt on saving. Also disable copying of cell values.
For this part, I don't know if this is possible, but you would need to override user access to VBA editor, so they couldn't walk around your add-in protections and also disable the access to options menu to disable the Add-in. That can be achieved by overwriting the Excels shortcut keys and hiding the menu bar.
Again, for the last part, I don't know if it's actually possible to limit user access to VBA editor. I think it would be pretty similar as limiting access to Add-in controls by overriding ALT+F11 shortcut and just hiding the developers tab as a whole.
Well that's my two cents, good luck!

Related

Communication between VSTO and .XLL

In Visual Studio, I have a solution. In that solution I have 2 projects. One is a VSTO so that we can make a plugin for Excel. The other project is for creating a .xll file so that we can have Custom Functions.
The VSTO helps us create a login system on excel so that they can do certain things.
However, since we only want our users to be able to use our custom functions they have to log in. I think that these 2 projects can't communicate directly so the .xll addin wouldn't know if a user is logged in or not.
Is there anyway for these 2 projects to communicate? Perhaps via a middle-man like a class with static variables?
EDIT:
More information:
Both projects are written in C# code. I was able to do that for the .xll file by using ExcelDNA.
So if there's any way that I can create maybe a C# class that can coordinate or share data between the two projects that would be really great. Since login data isn't the only thing that we want to share.
I'm hoping in the class there would be a static boolean variable holding whether the user is logged in. So the VSTO could set the boolean value and the .xll could get it.
You could add a hidden function [ExcelFunction(IsHidden=true)] to the .xll, which you can call from the VSTO add-in with Application.Run.
I would use a licensing system that let the user validate a licence key. This process can takes place within the VSTO. Then I would use two checks :
if in the VSTO the licence key is validated then load the xll, otherwise do not load it.
within the xll, by using relative path, I would locate and check a second time the licence key (to prevent that the user loads directly the xll).
It just requires to be able to check the licence key in C# (VSTO) and C (Xll), ie to have a validation key algorithm implemented in both languages.

Without opening, how can I read OpenXML document properties of password protected files programmatically?

I have an application which generates passwords for .xlsx files algorithmically based on custom document properties. Persistence of the properties is ensured by setting the openxmlencryptproperty key to false. As such, the file content is password protected, but the properties are visible (via explorer).
The intended functionality is that the password is recalculated from the properties before attempting to open the workbook. Unfortunately, the only methods I can find to get at these properties involve opening the workbook (either via System.IO.Packaging or OpenXml.Packaging).
Clearly explorer is able to read these properties without actually opening the file. How can I replicate this behaviour in C#? (perhaps invoking an instance of explorer?).
After various attempts to leverage the properties using the Microsoft.WindowsAPICodePack-Shell API, the only way I was able to get to custom properties was via the StructuredStorage API, as demonstrated in the snippet provided in the second part of this accepted answer to a similar question.

Cannot disable display alerts in Microsoft Publisher using Office Interop

I am using Microsoft.Office.Interop to open publisher from c# code. The issue is that unlike other office apps it does not provide the option to stop display alerts. Is there a way around this or a reason?
This is the way it is used for Word / Excel:
Microsoft.Office.Interop.[OFFICE_APP].Application app = new Microsoft.Office.Interop.[OFFICE_APP].Application();
app.DisplayAlerts = false;
You are correct that there is no option to disable alerts. What alert exactly is it that you need to supress?
For instance, if your Publisher file contains macros, you can avoid the warning regarding macros by setting Application.AutomationSecurity (https://msdn.microsoft.com/EN-US/library/office/ff939580.aspx) to msoAutomationSecurityForceDisable (https://msdn.microsoft.com/en-us/library/microsoft.office.core.msoautomationsecurity.aspx). This is the only alert I've ever needed to supress personally, but I'd be interested to know if you've run into another one.
I'm not sure whether there is a reason, but there are many discrepancies between the different object models in Microsoft Office, so perhaps you simply shouldn't expect it to work the same. For example, Excel doesn't have any Tags collection, whereas Publisher and Word do, but they're implemented quite differently.

How can I control Excel Add-Ins from a C# Winforms application?

I want to control an Excel Add-In with C# from a Windows Forms Application.
This is what I have so far:
var excelAddin = excelApp.AddIns.Add("C:/.../NWPredict.xlam", Type.Missing);
MessageBox.Show("Predict: " + excelAddin.FullName);
This code works, but how would I start the Add-In and change its settings?
If you want to permanently change settings of an add-in then you would need to Open this file, not attach it as an add-in to the currently running instance of Excel. (This isn't usually done programmatically though.)
Perhaps you should clarify because, to me, an add-in is supposed to be "complete". If any settings need to be changed when the add-in is in use, then the add-in itself should make these settings available to the Excel-instance, via exposed properties (or some other technique). But, again, these settings wouldn't be permanently saved (in the add-in itself).

Undo action for Excel addin

How does undo work in Interop.excel? C# - making a addin in VS
I imagine the following:
I register a undo-method on the stack (implemented by me).
I save the current state... where?
When the user uses undo (ctrl-z), my undo-method gets called, and I restore the previous state with the data that I stored.
Can't get any good info on this though. Maybe it works totally different?
You could look at NetOffice http://netoffice.codeplex.com/ They are tutorials how to create your own Add-In. You can work with VSTO, download the NetOffice .dll which you need to write your own Excel Add-In and to design it as you wish and implement your functionalities.

Categories

Resources