I created a userform in an *.xlam add-in and created a new commandbar and button in the IDE, but when I click the button, the user form is opened in Excel, and focus is forced away from the IDE. Is there a way to open the user form in the IDE instead of the host application without resorting to a .Net COM Add-in?
Here is the code that creates the commandbar and button and handles the button click event.
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
P.S. You may need this line of code to remove the commandbar...
Application.VBE.CommandBars("VBIDE").Delete
.Net COM Add-in
was just too much :D – Barchan