When we work on a Macro enabled excel file with digital certificate, most of us faced the issue - “There was a problem with the digital certificate. The VBA project could not be signed. The signature will be discarded”. Today we will look a little deeper into the issue.
Problem
When trying to save the excel after
assigning a digital certificate to the VBA project , receiving the
following exception
“There was a problem with the digital certificate. The VBA project could
not be signed. The signature will be discarded”.
Repro the issue:
1. Launch the Visual Studio Command prompt
Start
-> All Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools
-> Visual Studio Command Prompt
2.
Create the certificate authority using the makecert
makecert -n "CN=TestCA"
-cy authority -a sha1 -sv "TestCAPK.pvk" -r "TestCA.cer"
Specify the password.
"Sample@123".
2.
Create the certificate
makecert -n
"CN=TestCA" -ic
"TestCA.cer" -iv "TestCAPK.pvk" -a sha1 -sky signature
-sv
"TestTestPK.pvk" "TestTestCert.cer"
Specify the password.
"Test@123"
3.
Now combine the private key and certificate to pfx file
pvk2pfx -pvk
"TestTestPK.pvk" -spc "TestTestCert.cer" -pfx
"TestTestCert.pfx" -pi Test@123
Now
open the certificate store and import the certificate
"TestTestCert.cer" into Personal node. Remember the digital
certificates appearing under the VBA editor window is from Personal node only.
Now,
try to use the certificate in your Macro.
1. Open Excel sheet
2. Click on Developer Tab
3. Select Visual Basic under Code section
4. Enter the Macro Code in Microsoft Visual basic for
Applications windows

5. Now, select the Digital Signature option from Tools menu.

6. This will open the Digital Signature Window. Select Choose to
Select new certificate

7. Select our certificate from the Select a Certificate window.

8. Click OK
9. Click OK in Digital Signature Window.
10. Now Save the Macro enabled Excel. This will throw the
exception
Solution
Now,
go back to the certificate store and import the pfx file into Personal node.
Open the VBA editor window
from the Excel sheet and try to sign using the Digital Signature. This time
excel will save successfully.