1
votes

I currently have a management program (written in .NET 4.5) that packages up Excel workbooks for download to our customers. The workbooks are saved as .XLS (requirement). I'm looking for a method (either via Excel 2010 VBA or .NET 4.5 C#, preferably C#) of programmatically checking if an Excel workbook has been digitally signed.

EDIT: I was mistaken regarding the requirements. I need to be looking for the digital signature on the VBA/Macro side of the house.

For example: open an excel workbook click the Developer tab, click 'Tools', click 'Digital Signature'

1

1 Answers

2
votes

You can do it with Microsoft Office Tools. In the assembly Microsoft.Office.Tools.Excel.v4.0.Utilities you will find that the WorkbookBase class has a Signatures property that returns a collection. WorkbookBase documentation is here. In particular Signatures is defined as

Gets the collection of digital signatures for the workbook

Since it is a collection you can iterate over it or use the Count property to determine how many digital signatures are present. The collection is a SignatureSet and documentation on it can be found in the documentation at this link. Count is defined as:

Returns an Integer indicating the number of items in the specified collection.

You can use the regular Microsoft.Office.Interop.Excel assembly and the normal Workbook class to determine if the VBA code has been signed via the VBASigned property on a Workbook object. Information on that property can be found here.

Gets a value that indicates whether the Visual Basic for Applications project for the workbook has been digitally signed.

Assuming you are using C# and the Microsoft.Office.Interop.Excel assembly has been added as a reference in your VS C# project then code like this would open your workbook and get the VBASigned boolean flag:

using Excel = Microsoft.Office.Interop.Excel;

[snip]
Excel.Application exApp = new Excel.Application();
Excel.Workbook exWbk = exApp.Workbooks.Open(@"c:\path\to\your\workbook.xls");
Boolean signed = exWbk.VBASigned;

exWbk.Close();
// When finished make sure we release the Interop COM objects we used
Marshal.ReleaseComObject(exWbk);
Marshal.ReleaseComObject(exApp);