我在 *.xlam 加载项中创建了一个用户表单,并在 IDE 中创建了一个新的命令栏和按钮,但是当我单击该按钮时,用户表单在 Excel 中打开,并且焦点被迫离开 IDE。有没有办法在 IDE 而不是主机应用程序中打开用户表单而不使用 .Net COM 插件?
这是创建命令栏和按钮并处理按钮单击事件的代码。
Option Explicit
Public WithEvents cmdBarEvents As VBIDE.CommandBarEvents
Private Sub Class_Initialize()
CreateCommandBar
End Sub
Private Sub Class_Terminate()
Application.VBE.CommandBars("VBIDE").Delete
End Sub
Private Sub CreateCommandBar()
Dim bar As CommandBar
Set bar = Application.VBE.CommandBars.Add("VBIDE", MsoBarPosition.msoBarFloating, False, True)
bar.Visible = True
Dim btn As CommandBarButton
Set btn = bar.Controls.Add(msoControlButton, , , , True)
btn.Caption = "Show Form"
btn.OnAction = "ShowForm"
btn.FaceId = 59
Set cmdBarEvents = Application.VBE.Events.CommandBarEvents(btn)
End Sub
Private Sub cmdBarEvents_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
CallByName Me, CommandBarControl.OnAction, VbMethod
End Sub
Public Sub ShowForm()
Dim frm As New UserForm1
frm.Show
End Sub
PS您可能需要这行代码来删除命令栏...
Application.VBE.CommandBars("VBIDE").Delete