none
CommandBar Button only works once RRS feed

  • Question

  • Hi everyone, can you give me some opinion on what's wrong with my code? Any insights would be appreciated, thanks!

    PS: Just starting to create an Outlook addIn... (Newbie here)

    Public Class ThisAddIn
    
        Private Sub ThisAddIn_Startup(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) Handles Me.Startup
            Dim cmdButton As Object
            cmdButton = CreateCommandBarButton(Application.ActiveExplorer.CommandBars)
        End Sub
    
        Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
                                       ByVal e As System.EventArgs) Handles Me.Shutdown
    
        End Sub
        Public Function CreateCommandBarButton(ByVal oBars As Office.CommandBars) As Office.CommandBarButton()
            On Error Resume Next
            Dim oMenu As Office.CommandBar
            Dim oBtn As Office.CommandBarButton
            Const BAR_NAME As String = "thisMenu"
            Const CMD_NAME As String = "thisButton"
    
            oMenu = oBars(BAR_NAME)
            If oMenu Is Nothing Then
                oMenu = oBars.Add(BAR_NAME, Office.MsoBarPosition.msoBarTop, , True)
                oBtn = oMenu.Controls.Add(Office.MsoControlType.msoControlButton, , CMD_NAME, , True)
                oBtn.Caption = CMD_NAME
                oBtn.Tag = CMD_NAME
                oBtn.Style = Office.MsoButtonStyle.msoButtonCaption
    
            Else
                oBtn = oMenu.FindControl(, , CMD_NAME)
                If oBtn Is Nothing Then
                    oBtn = oMenu.Controls.Add(Office.MsoControlType.msoControlButton, , CMD_NAME, , True)
                End If
            End If
    
            With oBtn
    
                AddHandler oBtn.Click, AddressOf oBtn_Click
            End With
            oMenu.Visible = True
            CreateCommandBarButton = oBtn.Application
        End Function
        Private Sub oBtn_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
            MsgBox("clicked")
        End Sub
    End Class



    • Edited by Khem Oco Monday, September 10, 2012 12:54 AM
    Monday, September 10, 2012 12:52 AM

Answers

  • Hi Khem Oco

    Just move the declaration outside the This_Startup method, that's all you need to do. A variable declared at the class level is accessible to all methods in that class.

      Dim cmdButton As Office.CommandBarButton
     
      Private Sub ThisAddIn_Startup(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) Handles Me.Startup
            cmdButton = CreateCommandBarButton(Application.ActiveExplorer.CommandBars)
        End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Khem Oco Wednesday, September 12, 2012 10:04 PM
    Monday, September 10, 2012 7:40 AM
    Moderator

All replies

  • I've run a research through the forums and found out that the button must be created inside the class not on the startup, but how will I do it if it needs to call my function CreateCommandButton? :/
    Monday, September 10, 2012 1:00 AM
  • Hi Khem Oco

    Just move the declaration outside the This_Startup method, that's all you need to do. A variable declared at the class level is accessible to all methods in that class.

      Dim cmdButton As Office.CommandBarButton
     
      Private Sub ThisAddIn_Startup(ByVal sender As Object, _
                                      ByVal e As System.EventArgs) Handles Me.Startup
            cmdButton = CreateCommandBarButton(Application.ActiveExplorer.CommandBars)
        End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Khem Oco Wednesday, September 12, 2012 10:04 PM
    Monday, September 10, 2012 7:40 AM
    Moderator