Digitally Sign a VBA Code Project with Certificate
You can digitally sign your VBA code project using Aspose.Cells with its Workbook.VbaProject.Sign() method. Please follow these steps to check if your excel file is digitally signed with a certificate.
- Click Visual Basic from the Developer tab to open Visual Basic for Applications IDE
- Click Tools > Digital Signatures… of Visual Basic for Applications IDE
and it will show the Digital Signature Form showing if the document is digitally signed with a certificate or not.
Digitally Sign a VBA Code Project with Certificate in C#
The following sample code illustrates how to make use of Workbook.VbaProject.Sign() method. Here are the input and output files of the sample code. You can use any excel file and any certificate to test this code.
- Source Excel file used in the sample code.
- Sample pfx file to create Digital Signature. Please install it on your computer to run this code. Its password is 1234.
- Output Excel file generated by the sample code.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String password = "1234"; | |
String pfxPath = sourceDir + "sampleDigitallySignVbaProjectWithCertificate.pfx";; | |
String comment = "Signing Digital Signature using Aspose.Cells"; | |
// Load the certificate into an instance of InputStream | |
InputStream inStream = new FileInputStream(pfxPath); | |
// Create an instance of KeyStore with PKCS12 cryptography | |
KeyStore inputKeyStore = KeyStore.getInstance("PKCS12"); | |
// Use the KeyStore.load method to load the certificate stream and its password | |
inputKeyStore.load(inStream, password.toCharArray()); | |
inStream.close(); | |
// Create an instance of DigitalSignature and pass the instance of KeyStore, password, comments and time | |
DigitalSignature signature = new DigitalSignature(inputKeyStore, password, comment, | |
DateTime.getNow()); | |
Workbook wb = new Workbook(sourceDir + "sampleDigitallySignVbaProjectWithCertificate.xlsm"); | |
wb.getVbaProject().sign(signature); | |
wb.save(outputDir + "outputDigitallySignVbaProjectWithCertificate.xlsm"); |