Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

How to check for whether toolbar contain particular button?

Hi,

Could anyone please tell me how to check whether particular button (named  "My &Permanent Button" in given code) exists in toolbar or not before adding it. I want to put this check before adding any button into toolbar using below line.

Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "891", , True)

Thanks.
Private Sub CreateToolBar()
    Dim testIt As Boolean
    If out_App.Explorers.Count = 0 Then
        Exit Sub
    End If
     
    Const TOOLBARNAME = "Permanent Toolbar Testing2"
    On Error Resume Next
    testIt = Not out_App.ActiveExplorer.CommandBars(TOOLBARNAME) Is Nothing
    If testIt Then
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars.Item(TOOLBARNAME)
    Else
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars.Add(TOOLBARNAME, msoBarTop, False, False)
    End If
        
    Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "891", , True)
    With MyButton
        .BeginGroup = True
        .Caption = "My Permanent Button"
        .Enabled = True
        .OnAction = "!<PermToolbarTesting2.Connect>"
        .Tag = "891"
        .FaceId = 362
        .Style = 3
        .Visible = True
    End With
    MyCommandBar.Visible = True
End Sub

Open in new window

0
deshaw
Asked:
deshaw
  • 11
  • 9
1 Solution
 
Chris BottomleyCommented:
Hello deshaw,

I hate i t theat the solution I gave was incomplete.  I proved the failure myself once just now but couldn't reproduce it but I think this ought to resolve it as it forces the value false ... ought to happen anyway - do leave the Question open though till you are confident - someone else may find the facts!

Regards,

chris_bottomley
Private Sub CreateToolBar()
Dim testit As Boolean
Set out_app = Application
    If out_app.Explorers.count = 0 Then
        Exit Sub
    End If
    
    Set myExpl = out_app.ActiveExplorer
    Const TOOLBARNAME = "Permanent Toolbar Testing1"
    On Error Resume Next
    testit = False
    testit = Not out_app.ActiveExplorer.CommandBars(TOOLBARNAME) Is Nothing
    If testit Then out_app.ActiveExplorer.CommandBars(TOOLBARNAME).Delete
     Set MyCommandBar = myExpl.CommandBars.Add(TOOLBARNAME, msoBarTop, False, True)
    Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "890", , True)
    With MyButton
        .BeginGroup = True
        .Caption = "My Temporary Button"
        .Enabled = True
        '.OnAction = "!<PermToolbarTesting1.Connect>"
        .OnAction = "!<Project1.deleteme.connect>"
        .Tag = "890"
        .FaceId = 362
        .Style = 3
        .Visible = True
    End With
    MyCommandBar.Visible = True
End Sub

Open in new window

0
 
deshawAuthor Commented:
Hey Chris, I think you have misunderstood me. Your solution were absolutely correct but I am facing issue when user have setup such that two Outlook windows simultaneously opens during startup.
 
If you follow below steps, two windows will open simultaneously during startup.  
1.      Open Outlook  
2.      Open Calendar in new window  
3.      Close Outlook  
4.      Close Calendar  
5.      Reopen Outlook --> You will have two windows opened automatically. Outlook will be confused about where to include the toolbar so you can expect all possible combinations. For example, you will not have Outlook toolbar at all or you will have multiple buttons on either of one window.  Weird!! but it is true....Try it....

I just want to avoid same button two times in same toolbar and hence, I want to put a check before adding button into toolbar. In other words, before executing below code, I want  to make sure that "MyCommandBar" toolbar doesn't contain "My Permanent Button" already.

Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "891", , True)
    With MyButton
        .BeginGroup = True
        .Caption = "My Permanent Button"
        .Enabled = True
        .OnAction = "!<PermToolbarTesting2.Connect>"
        .Tag = "891"
        .FaceId = 362
        .Style = 3
        .Visible = True
    End With

I hope now I am clear.

Thanks.
0
 
Chris BottomleyCommented:
Haven't had much luck refining it as an absolute.  I have seen your observation although not regarding variability it always seems to come up on the calendar for me.

I do wonder if it's worth examing the active explorer and using that as an additional check:

application.activeexplorer.Caption

i.e. you startup whichever window you have set as default and only add the menu there:

if application.Explorers("Unread or Flagged - Microsoft Outlook") is nothing then
    do nothing
else
    with application.Explorers("Unread or Flagged - Microsoft Outlook").commandbars
        ...
    end with
end if

Chris
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
deshawAuthor Commented:
Hmm...thats happen with me also. For me it always come on Calender but other add-in we have that comes always on main Outlook window.

The logic you have given doesn;t sound perfect. How would you identify that the toolbar on "foo" explorer caption doesn;t contain button "myFooButton"?

You logic might not add toolbar at all. The question is nailed down to button existence in toolbar rather than toolbar existence in explorer.

I hope I am clear. Please let me know if I misunderstood you.

Thanks.
0
 
Chris BottomleyCommented:
I have done a few quick tests calling the sub from teh application_startup and at first sight the following looks plausible:

Chris
Sub CreateToolBar()
Dim testit As Boolean
Dim expl As Explorer
Dim testExpl As Explorer
Set out_app = Application

    
    For Each testExpl In out_app.Explorers
        If testExpl.Caption = "Unread or Flagged - Microsoft Outlook" Then
            Set expl = testExpl
            Exit For
        End If
    Next
    
    If expl Is Nothing Then Exit Sub
    Const TOOLBARNAME = "Permanent Toolbar Testing1"
    On Error Resume Next
    testit = False
    testit = Not expl.CommandBars(TOOLBARNAME) Is Nothing
    If testit Then expl.CommandBars(TOOLBARNAME).Delete
     Set MyCommandBar = expl.CommandBars.Add(TOOLBARNAME, msoBarTop, False, True)
    Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "890", , True)
    With MyButton
        .BeginGroup = True
        .Caption = "My Temporary Button"
        .Enabled = True
        '.OnAction = "!<PermToolbarTesting1.Connect>"
        .OnAction = "!<Project1.deleteme.connect>"
        .Tag = "890"
        .FaceId = 362
        .Style = 3
        .Visible = True
    End With
    MyCommandBar.Visible = True
End Sub

Open in new window

0
 
deshawAuthor Commented:
What is  "Unread or Flagged - Microsoft Outlook" caption. I don;t see this caption on any of outlook window. If I open Calender into new Outlook window then you function will exit without adding toolbar. Right?

Thanks.
0
 
Chris BottomleyCommented:
Unread or Flagged - Microsoft Outlook is the caption displayed on my main outlook window ... it is in fact a search folder but teh application header displays a string that relates to the startup folder and it's always teh same for a startup instance.  YOurs could for exampole be something like Inbox - Microsoft Outlook be careful though the case is critical.

Select the relevnt startup window where you want the button then in the VBE type:
application.activeexplorer.Caption

Replace the string in my code for the startup window with this string.

Chris
0
 
Chris BottomleyCommented:
The function I uploaded will for every window look for the required explorer and if found then it checks for a pre-existing menu to delete and then adds the new menu bar.

Hence in teh scenario you gave it has two tries at adding teh menu to the required window.

1. When that window actually opens
2. When another window opens on startup it looks for the required window and repeats the check ... but only one will be there due to the functionality


Chris
0
 
deshawAuthor Commented:
The startup event will happen only once so I really don;t see my scenario can be solved by just handling startup event. I am sure that your code will not add toolbar when I right click on Calender and open it in new window as it will not match "nread or Flagged - Microsoft Outlook" which is happens only during startup as per you.

 I think the perfect solution would be checking for button directly for toolbar object like you done it for Commandbar using "testIt". Isn;t something like possible? I tried it but doesn;t work.
 
    testit = False
    testit = Not MyCommandBar.Controls("My Temporary Button")Is Nothing
    If Not testit Then
                 Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "890", , True)
   With MyButton
        .BeginGroup = True
        .Caption = "My Temporary Button"
        .Enabled = True
        .OnAction = "!<Project1.deleteme.connect>"
        .Tag = "890"
        .FaceId = 362
        .Style = 3
        .Visible = True
    End With
   Else
                  MsgBox "Button Exists"
    End If

Thanks.
0
 
Chris BottomleyCommented:
I have interpreted your requirement as only one copy of the toolbarmenu and that it should be on a specific screen.  This is what I have been investigating and posted the potential solution for.

Are you saying that this is not the case and that you actually want it displayed in every explorer that opens?

Chris
0
 
Chris BottomleyCommented:
If this latter then:

Add a line to the top of thisOutlookSession as:
Dim WithEvents olkCalendar As Outlook.items

Modify application_startup to add a line as below:
    Set olkExplorer = Application.Explorers

Add a sub in thisoutlooksession as olkExplorer_NewExplorer in the snippet.

Used the modified CreateToolBar as below.

Restart outlook
Dim WithEvents olkCalendar As Outlook.items

Private Sub Application_Startup()
    Set olkExplorer = Application.Explorers
    CreateToolBar
End Sub

Private Sub olkExplorer_NewExplorer(ByVal Explorer As Explorer)
    CreateToolBar
End Sub

Sub CreateToolBar()
Dim testit As Boolean
Dim expl As Explorer
Set out_app = Application
Const TOOLBARNAME = "Permanent Toolbar Testing1"
    
    For Each expl In out_app.Explorers
        On Error Resume Next
        testit = False
        testit = Not expl.CommandBars(TOOLBARNAME) Is Nothing
        On Error GoTo 0
        If testit Then expl.CommandBars(TOOLBARNAME).Delete
         Set MyCommandBar = expl.CommandBars.Add(TOOLBARNAME, msoBarTop, False, True)
        Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "890", , True)
        With MyButton
            .BeginGroup = True
            .Caption = "My Temporary Button"
            .Enabled = True
            '.OnAction = "!<PermToolbarTesting1.Connect>"
            .OnAction = "!<Project1.deleteme.connect>"
            .Tag = "890"
            .FaceId = 362
            .Style = 3
            .Visible = True
        End With
        MyCommandBar.Visible = True
    Next
    
End Sub

Open in new window

0
 
deshawAuthor Commented:
I absolutely appreciate your efforts. But I don't know you are not getting or I am not clear. Well, I have given my complete code here and also attached the things that I want to prevent.

You can see that in attached toolbar, two buttons get added into same toolbar. This will happen as I called "CreateToolbar" two times from OnStartupevent just to reproduce the behavior I mentioned earlier. see below:

Private Sub AddinInstance_OnStartupComplete(custom() As Variant)
    Set myColExpl = out_App.Explorers
    If out_App.Explorers.Count > 0 Then
        Call CreateToolBar
        Call CreateToolBar
    End If
End Sub

Now if some how some case(e.g two simultaneous windows open during startup) calls two times CreateToolbar, the second "CreateToolbar" call should not add DUPLICATE button and should be return.

Thanks.

Option Explicit
Public out_App As Object
Public WithEvents MyButton As Office.CommandBarButton
Public MyCommandBar As Office.CommandBar
Public WithEvents myColExpl As Outlook.Explorers
Public WithEvents myExpl As Outlook.Explorer

Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
    Set out_App = Application
End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode _
   As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
    Set MyButton = Nothing
    Set MyCommandBar = Nothing
End Sub

Private Sub AddinInstance_OnStartupComplete(custom() As Variant)
    Set myColExpl = out_App.Explorers
    If out_App.Explorers.Count > 0 Then
        Call CreateToolBar
        Call CreateToolBar
    End If
End Sub

Private Sub myColExpl_NewExplorer(ByVal Explorer As Outlook.Explorer)
    If myExpl Is Nothing Then
        Set myExpl = Explorer
    End If

    If out_App.Explorers.Count > 0 Then
        Call CreateToolBar
    End If
End Sub

Private Sub myExpl_Close()
    If out_App.Explorers.Count < 1 Then
        Set myExpl = Nothing
        Set myColExpl = Nothing
    End If
End Sub

Private Sub MyButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    MsgBox "button clicked"
End Sub

Private Sub CreateToolBar()
    Dim testIt As Boolean
    If out_App.Explorers.Count = 0 Then
        Exit Sub
    End If
     
    Const TOOLBARNAME = "Permanent Toolbar Testing2"
    On Error Resume Next
    testIt = Not out_App.ActiveExplorer.CommandBars(TOOLBARNAME) Is Nothing
    If testIt Then
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars(TOOLBARNAME)
    Else
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars.Add(TOOLBARNAME, msoBarTop, False, False)
    End If
'
'
' HERE YOU SHOULD PUT CHECKING FOR BUTTON EXISTENCE CHECK 
'
'
    Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "891", , True)
    With MyButton
        .BeginGroup = True
        .Caption = "My &Permanent Button"
        .Enabled = True
        .OnAction = "!<PermToolbarTesting2.Connect>"
        .Tag = "891"
        .FaceId = 362
        .Style = 3
        .Visible = True
    End With
    MyCommandBar.Visible = True
End Sub

Open in new window

myToolbar.JPG
0
 
Chris BottomleyCommented:
Please check the sub I posted.  To ensure a single copy it deletes the toolbar if found and then inserts it whereas your code creates it irrespective.

The most recent code I put up will ensure a single copy of the toolbar in every explorer.  The previous submission ensures a single copy of the toolbar in a specific instance.

The key difference to what you are reporting is I think that the CreateToolBar instance you are using is different to that I supplied in this thread.

Of course if I am still missing the point then please try and educate me but first off please try the code I supplied so we are working from a common baseline.


Chris
0
 
deshawAuthor Commented:
Chris, your code works fine but I don't want to delete toolbar because if I do that then toolbar will reappear when I restart the outlook even though I hidden it. and also it will appear at default position even though I repositioned it.

Hence, I kept toolbar permanent so that Outlook remembers hide/reposition event. Moreover, buttons are temporary to associate click events every time toolbar gets added.

My question is very simple: During adding button into my toolbar, I want to perform a check if button is exists or not. If it exists then proceed without adding it.

Thanks.
0
 
Chris BottomleyCommented:
If the wish is merely to see if the command bar exists then noting the difficulties across versions the following function will do that:

Called for example as:

?testCBAvailable(TOOLBARNAME)

Chris
Function testCBAvailable(cbName As String) As Boolean
Dim CB As Object

    For Each CB In Application.ActiveExplorer.CommandBars
        If LCase(CB.Name) = LCase(cbName) Then
            testCBAvailable = True
            Exit For
        End If
    Next

End Function

Open in new window

0
 
deshawAuthor Commented:
Chris, I don't wan;t to check if command bar available. I am already doing it with below lines of code.

   testIt = Not out_App.ActiveExplorer.CommandBars(TOOLBARNAME) Is Nothing
    If testIt Then
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars(TOOLBARNAME)
    Else
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars.Add(TOOLBARNAME, msoBarTop, False, False)
    End If

Please understand my requirements. See my code. After above lines, I will have "MyCommandBar" object set to my toolbar(if not exists, it will add and set the object).

Now, after this step, I am adding button one by one. I want to check if button that is being added is already present in toolbar or not. If button is already exists then proceed without adding it. See below attached pseudo code for this.

I want the code for below lines in attached code:

'
' HERE YOU SHOULD PUT CHECKING FOR BUTTON EXISTENCE CHECK
' If button exists then make buttonExists = true
'

Thanks.

testIt = Not out_App.ActiveExplorer.CommandBars(TOOLBARNAME) Is Nothing
    If testIt Then
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars(TOOLBARNAME)
    Else
        Set MyCommandBar = Outlook.Application.ActiveExplorer.CommandBars.Add(TOOLBARNAME, msoBarTop, False, False)
    End If

Boolean buttonExists = false
'
' HERE YOU SHOULD PUT CHECKING FOR BUTTON EXISTENCE CHECK 
' If button exists then make buttonExists = true
'
'
If not buttonExists Then
    Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "891", , True)
End If

Open in new window

0
 
Chris BottomleyCommented:
As you have seen I have missed the opoint throughout but I think I now follow:

The following tests for the CB and creates it isf missing.  It then tests the CB for the button and if missing creates it.

Does this better meet the request.

Chris
Sub CreateToolBar()
Dim testIt As Boolean
Dim expl As Explorer
Set out_App = Application
Dim MyCommandBar As Object
Const TOOLBARNAME = "Permanent Toolbar Testing1"
Const BUTTONNAME = "My Temporary Button"
    
    For Each expl In out_App.Explorers
        On Error Resume Next
        testIt = False
        testIt = Not expl.CommandBars(TOOLBARNAME) Is Nothing
        On Error GoTo 0
        If Not testIt Then
            expl.CommandBars.Add TOOLBARNAME, msoBarTop, False, True
        End If
        Set MyCommandBar = expl.CommandBars(TOOLBARNAME)
        If testBtnAvailable(MyCommandBar, BUTTONNAME) Then
            'Button ALready exists
        Else
            'Button Does NOT exist
            Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "890", , True)
            With MyButton
                .BeginGroup = True
                .Caption = BUTTONNAME
                .Enabled = True
                '.OnAction = "!<PermToolbarTesting1.Connect>"
                .OnAction = "!<Project1.deleteme.connect>"
                .Tag = "890"
                .FaceId = 362
                .Style = 3
                .Visible = True
            End With
            End If
        MyCommandBar.Visible = True
    Next
    
End Sub
Function testBtnAvailable(Cb As Object, btnName As String) As Boolean
 
    For Each btn In Cb.Controls
        If btn.Type = msoControlButton Then
            If LCase(btn.Caption) = LCase(btnName) Then
                testBtnAvailable = True
                Exit For
            End If
        End If
    Next
 
End Function

Open in new window

0
 
deshawAuthor Commented:
Took long to get there. Thanks. :)
0
 
Chris BottomleyCommented:
>>> Took long time to get there.

Yah only minutes to do the solution and forever till I understood the Question, (which was clear enough I just read what I thought into it) apologies again for that and I hope it didn't frustrate you too much!

Chris
0
 
deshawAuthor Commented:
Never mind Chris. I got the solution. That what matters at the end. :-)
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now