Placing Buttons in VBE Tools

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Placing Buttons in VBE Tools

Post by kpark91 »

Hi, I'm currently trying to learn from this code [Creates BB or HTML code from selected text in VBE or the entire procedure]
but I am already stuck in creating buttons in VBE tool.

Code: Select all

'Put the buttons in the VBE Tools menu
Public Sub Initialize()
    Dim Btn As CommandBarButton, Obj As Object, cBtn As clsBtn
    Dim i As Long, j As Long
    Terminate
    Set Col = New Collection
    For j = 1 To 2
        On Error GoTo err_h
        Set Obj = Choose(j, Application.VBE.CommandBars.FindControl(ID:=30007), Application.VBE.CommandBars("Code Window"))
        With Obj
            For i = 1 To 4
                Set Btn = .Controls.Add(msoControlButton)
                With Btn
                    .Caption = Choose(i, "Convert selected code as &HTML", "Convert current procedure as &HTML", "Convert selected code as &BB code", "Convert current procedure as &BB code")
                    .BeginGroup = Choose(i, True, False, True, False)
                    .Tag = AppCode
                    .OnAction = ThisWorkbook.Name & "!Create" & IIf(i <= 2, "HTML", "BB")
                    If i Mod 2 = 0 Then
                        .Parameter = 1
                    Else
                        .Parameter = 0
                    End If
                    .FaceId = Choose(i, 2478, 0, 1557, 0)
                    .Style = msoButtonIconAndCaption
                End With
                Set cBtn = New clsBtn
                Set cBtn.BtnEvents = Application.VBE.Events.CommandBarEvents(Btn)
                Col.Add cBtn
            Next i
        End With
    Next j
    Exit Sub
err_h:
    If Err.Number = 1004 Then
        MsgBox "Access to the VB project is not trusted", vbCritical, AppName
        ThisWorkbook.Close False
    Else
        MsgBox Err.Number & ": " & Err.Description
        Resume Next
    End If
End Sub
This is just a one sub of many in this program and I'm not asking for the entire translation/interpretation of this whole code.

In this code, I am not understanding what

Code: Select all

.OnAction = ThisWorkbook.Name & "!Create" & IIf(i <= 2, "HTML", "BB")
does...
Doesn't .OnAction just looks for a sub in the procedure and runs it?


To Administrator: If you need this BB/HTML convertor, PM me and I'll send the code or the whole add-in to you :)
I don't have one

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Placing Buttons in VBE Tools

Post by HansV »

.OnAction is a property of a toolbar button/menu item of type String. It doesn't do anything in itself. It contains the name of the macro that will be run later, when the user clicks on the toolbar button/menu item.

In this example, the macro CreateHTML or CreateBB is assigned to the button.
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Placing Buttons in VBE Tools

Post by kpark91 »

Ohhhhhhh.

So everytime for .OnAction property,
I must specify the workbook's name and then the sub's name?

You did it again xD
Thank you
I don't have one

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Placing Buttons in VBE Tools

Post by HansV »

In many situations, it is enough to specify only the macro name, but to avoid problems if the same macro name exists in more than one workbook, you can prefix it with the name of the workbook followed by an exclamation mark !
You only have to set the value of OnAction once, after creating the toolbar button/menu item (unless you want to change its action later on).
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Placing Buttons in VBE Tools

Post by kpark91 »

Thank you for the kind response :)

If it is not too much of a bother,
What does

Code: Select all

Choose(j, Application.VBE.CommandBars.FindControl(ID:=30007), Application.VBE.CommandBars("Code Window"))
exactly do?

and for FindControl(ID:=30007), how do they know what number to use? (30007, blah blah)
I don't have one

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Placing Buttons in VBE Tools

Post by HansV »

This code modifies both the Tools menu in the VBE and the right-click menu of module windows, and it tries to be a bit "clever" to avoid duplication of code.
The outermost loop

For j = 1 To 2
...
Next j

first modifies the Tools menu when j = 1, then the right-click menu when j = 2.

Choose(j, Application.VBE.CommandBars.FindControl(ID:=30007), Application.VBE.CommandBars("Code Window"))

looks at the value of j.

When j = 1, it returns Application.VBE.CommandBars.FindControl(ID:=30007). This is the Tools menu. How did they find that? The other way round, by typing the following in the Immediate window, then pressing Enter:

? Application.VBE.CommandBars("Menu Bar").Controls("&Tools").ID

This returns 30007, so the unique ID for the Tools menu is 30007. This is language-independent, so ID=30007 refers to the Outils menu in the French version of Office and to the Strumenti menu in the Italian version of Office.

When j = 2, the above expression returns Application.VBE.CommandBars("Code Window"), the right-click menu of the module window. Because end users never see the name of this menu, its name is not localized in foreign-language versions of Office, and Application.VBE.CommandBars("Code Window") works in all languages.
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Placing Buttons in VBE Tools

Post by kpark91 »

:O You're crazy...
How can you remember all those :P

Thank you very much for the explanation!!
I can really see myself improving in VBA language in this forum.

AWESOME forum btw.
We just need more people now :D
I don't have one

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Placing Buttons in VBE Tools

Post by HansV »

Eileen's Lounge is coming along nicely - we started just half a year ago, and we've been growing slowly but steadily. We're trying to be not just another discussion forum, but also a community where people know and respect each other.
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Placing Buttons in VBE Tools

Post by kpark91 »

I can really see that happening in the future.
For that, perserverance is the key to success :P

I was actually brought here through a link from some MVP (Microsoft Valuable Professional) website <I forget the name>
Are you in that company by any chance?
I don't have one

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Placing Buttons in VBE Tools

Post by HansV »

I'm just someone who likes to work with MS Office and VBA and who likes to help others and to learn from that... :smile:
Best wishes,
Hans