我想写一个简单的Excelmacros,为用户调用添加数字签名对话框。 我不想添加签名本身,只是显示添加数字签名对话框,以便用户不必自己查找它。 我正在search解决scheme,并了解这不能在本机Excel VBA中完成。 必须直接调用Windows Shell。 我怎么做?
你不要陈述你的Excel版本,但假设你有一个功能区UI的版本。 有几个选项 – 你可以使用流畅的UI控制标识符和这个代码:
Option Explicit Sub FindControlByFluentUIId() Dim objCtrl As CommandBarControl Dim lngId As Long On Error GoTo ErrHandler ' magic number of Add Digital Signature lngId = 13035 ' find that control in the command bars collection ' this line throws an error for some workbooks !? Set obj = Application.CommandBars.FindControl(Office.MsoControlType.msoControlButton, lngId) ' execute If Not obj Is Nothing Then obj.Execute Else MsgBox "Not found" End If End Sub ErrHandler: If Err.Number <> 0 Then Debug.Print Err.Description End If End Sub
代码的完整列表在这里: https : //www.microsoft.com/en-us/download/details.aspx?id=36798
如果由于某种原因您不知道ID,则可以手动搜索每个控制条的每个控制集合,以获得带有Caption
的控件,就像您正在查找的控件一样。 您最好使用Like
运算符进行通配符搜索,因为您可能不知道控制标题和位置是否便于键盘快捷方式的确切情况。
你可以尝试这样的事情:
Option Explicit Sub TestFindControl() Dim strCaptionWild As String Dim objCtrl As CommandBarControl ' use wildcards to help find the control strCaptionWild = "*add*a*digital*signature*" ' call the function to find by caption Set objCtrl = FindControl(strCaptionWild) ' execute on match If Not objCtrl Is Nothing Then Debug.Print "Command bar index: " & objCtrl.Parent.Index Debug.Print "Control index: " & objCtrl.Index Debug.Print "Real caption: " & objCtrl.Caption objCtrl.Execute Else MsgBox "Not found for caption: " & strCaptionWild End If End Sub Function FindControl(ByVal strCaption As String) As CommandBarControl Dim objCb As CommandBar Dim objCtrl As CommandBarControl Dim blnFound As Boolean On Error GoTo ErrHandler ' not found the control blnFound = False ' iterate command bars and their controls For Each objCb In Application.CommandBars For Each objCtrl In objCb.Controls ' use like operator check control caption vs input caption ' LIKE enables use of wildcard matching If LCase$(objCtrl.Caption) Like LCase$(strCaption) Then ' found it blnFound = True Exit For End If Next objCtrl If blnFound Then Exit For Next objCb Set FindControl = objCtrl Exit Function ErrHandler: Debug.Print Err.Description Set FindControl = Nothing End Function