Home » excel » vba – How to assign certificates to excel macros programmatically

vba – How to assign certificates to excel macros programmatically

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have developed some code which creates excel macros and each time after creation of new macro I want to digitally sign the macro programmatically. Can you let me know if there is any way using which I can create new digital certificates and assign those to macro programmatically. Thanks.

How to&Answers:

It is not possible to do this with VBA code. I think the best you could hope to do it would be by making Win32 API calls to simulate all the needed steps to make this happen. It would be sloppy but you could make it work. Basically, you would have open the needed windows and applications leveraging shortcut keys on the keyboard and for selecting the correct certs.

Some example code would be

Public Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
    ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Sub SetFocusFormulaBar()
    SetFocus FindWindowEx(FindWindow("XLMAIN", Application.Caption), _
        0&, "EXCEL<", vbNullString)
End Sub

http://www.cpearson.com/excel/FormulaBarShortcut.htm