Wednesday, July 23, 2008

Programmatically remove digital signatures from Excel document

Here is some sample code to remove all digital signatures from an Excel workbook. This can be run from a Windows app or Console app. I'm going to try to make it work within a SharePoint document library event handler. That should be interesting. Basically the idea is to have a document library that has a bunch of documents that are digitally signed and then every month get reset by removing all the digital signatures. Hope this runs in the event handler.

Microsoft.Office.Interop.Excel.Application eApp = null; Microsoft.Office.Interop.Excel.Workbook wb = null;

//gets our workbook
eApp = new Microsoft.Office.Interop.Excel.Application();
wb = eApp.Workbooks.Open(textBox1.Text, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

//makes it visible and activates the workbook

//eApp.Visible = true;
//wb.Activate();

//get signatures

Microsoft.Office.Core.SignatureSet sigs = wb.Signatures;

//remove all the signatures

foreach (Microsoft.Office.Core.Signature sig in sigs)
{ sig.Delete(); }

//save and close the form

wb.Save();
wb.Close(Type.Missing, Type.Missing, Type.Missing);
wb = null;
eApp = null;

3 comments:

Unknown said...

Thanks a lot for giving the code.I am facing a problem of this kind. I will certainly try it and will let you know.
public private key

Anonymous said...

thanks

public key infrastructure said...

I was searching for the code for removing all digital signatures from an Excel sheet.You really saved a lot of my time.I will try to use this.Thanks