none
How to remove or delete commandBar from outlook inspector window ? RRS feed

  • Question

  •    

    This is my code from this code i am succesfully adding button in Microsoft outlook inspector but when new mail open in outlook inspector (by double clicking in inbox) button sccesfully adding but if i click same mail again then two button adding in outlook inspector so before calling event i want to remove or delete previous button

    Means multyple instance of button created outlook inspector window

    How to remove button from Inspector ?

        
    Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
    
        Try
       Private MenuBar As Office.CommandBar    
       Private ButtonOne As Office.CommandBarButton
    
            Dim mailItem As outlook.MailItem = TryCast(Inspector.CurrentItem, outlook.MailItem)
            If Not (mailItem Is Nothing) Then
                Try
    
                    MenuBar = Inspector.CommandBars.Add("Mail", Office.MsoBarPosition.msoBarTop, False, True)
                    MenuBar.Visible = True
                    MenuBar.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize
    
                    ButtonOne = MenuBar.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)
                    'ButtonOne.Caption = "Archive"
                    'ButtonOne.Tag = "My Button"
                    Dim newIcon As Icon = My.Resources.iiMS_32x32
                    Dim newImageList As New ImageList
                    newImageList.Images.Add(newIcon)
                    Clipboard.SetDataObject(newImageList.Images(0), False)
                    With ButtonOne
                        .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                        .Caption = ButtonCaption
                        .FaceId = 65
                        .Tag = menuTag
                        ButtonOne.PasteFace()
                    End With
    
                    Try
                        AddHandler ButtonOne.Click, AddressOf ButtonClick
                    Catch ex As Exception
                        writelog("Error Occured in preview windows while adding a Handler, Error: " & ex.Message)
                        MessageBox.Show("Application Error, Please Check Error Log", strCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
    
                Catch ex As Exception
                    writelog(ex.Message)
                    writelog(ex.StackTrace)
                End Try
            End If
    
        Catch ex As Exception
            writelog(ex.Message)
            writelog(ex.StackTrace)
        End Try
    
    End Sub

    Tuesday, March 4, 2014 10:52 AM

Answers

  • Hi,

    To avoid the CommandBar Button to be added twice, you could add a judgment with CommandBars.FindControl Method before adding a new button in the code.

    I edit your code for your reference.

    Try
    
        MenuBar = Inspector.CommandBars.Add("Mail", Office.MsoBarPosition.msoBarTop, False, True)
        MenuBar.Visible = True
        MenuBar.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize
    
        If Inspector.CommandBars.FindControl(Office.MsoControlType.msoControlButton,
     System.Type.Missing, menuTag, True) Is Nothing Then
            ButtonOne = MenuBar.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)
            'ButtonOne.Caption = "Archive"
            'ButtonOne.Tag = "My Button"
            Dim newIcon As Icon = My.Resources.iiMS_32x32
            Dim newImageList As New ImageList
            newImageList.Images.Add(newIcon)
            Clipboard.SetDataObject(newImageList.Images(0), False)
            With ButtonOne
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = ButtonCaption
                .FaceId = 65
                .Tag = menuTag
                ButtonOne.PasteFace()
            End With
    
            Try
                AddHandler ButtonOne.Click, AddressOf ButtonClick
            Catch ex As Exception
                writelog("Error Occured in preview windows while adding a Handler, Error: " & ex.Message)
                MessageBox.Show("Application Error, Please Check Error Log", strCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    
    Catch ex As Exception
        writelog(ex.Message)
        writelog(ex.StackTrace)
    End Try



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 5, 2014 8:56 AM
    Moderator

All replies

  • Hello Sauru,

    First of all, starting from Outlook 2007 inspectors don't use command bars for their UI. Instead, you need to use Fluent UI. You can read more about the new UI in the series of articles in MSDN:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    Before adding a new button you need to check whether such button is already presented in the inspector and only then add a new one or skip it. For example, you can use the FindControl or FindControls methods of the CommandBars class.

    Tuesday, March 4, 2014 12:02 PM
  • Thanku for reply

    but how to find the ID of Command Bar in Inspector, i am not familier with outlook by seeing the above code

    Can you suggest how to find the ID ?


    • Edited by Sauru Tuesday, March 4, 2014 12:19 PM
    Tuesday, March 4, 2014 12:19 PM
  • Hello Sauru,

    Please take a look at the OL: How to Use CommandBars in Outlook Solutions article. It describes all the required steps in depth and provides a sample code.

    Tuesday, March 4, 2014 12:28 PM
  • Dear Sir

    I need for finding the ID for outlook Inspector not for explore.

    Thanku sir.

    Tuesday, March 4, 2014 12:39 PM
  • I am using this code but null exception is comming

          Private Sub RemoveMenubar()
            Try
                ' If the menu already exists, remove it.
                Dim foundMenu As Office.CommandBarPopup = _
                    Me.Application.ActiveInspector().CommandBars.ActiveMenuBar. _
                    FindControl(Office.MsoControlType.msoControlPopup, _
                    System.Type.Missing, menuTag, True, True)
                If foundMenu IsNot Nothing Then
                    foundMenu.Delete(True)
                End If

            Catch Ex As Exception
                writelog("Error Occured in RemoveMenubar, Error: " & Ex.Message)
                MessageBox.Show("Application Error, Please Check Error Log", strCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub


    • Edited by Sauru Tuesday, March 4, 2014 12:49 PM
    Tuesday, March 4, 2014 12:49 PM
  • Hi,

    To avoid the CommandBar Button to be added twice, you could add a judgment with CommandBars.FindControl Method before adding a new button in the code.

    I edit your code for your reference.

    Try
    
        MenuBar = Inspector.CommandBars.Add("Mail", Office.MsoBarPosition.msoBarTop, False, True)
        MenuBar.Visible = True
        MenuBar.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoCustomize
    
        If Inspector.CommandBars.FindControl(Office.MsoControlType.msoControlButton,
     System.Type.Missing, menuTag, True) Is Nothing Then
            ButtonOne = MenuBar.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)
            'ButtonOne.Caption = "Archive"
            'ButtonOne.Tag = "My Button"
            Dim newIcon As Icon = My.Resources.iiMS_32x32
            Dim newImageList As New ImageList
            newImageList.Images.Add(newIcon)
            Clipboard.SetDataObject(newImageList.Images(0), False)
            With ButtonOne
                .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                .Caption = ButtonCaption
                .FaceId = 65
                .Tag = menuTag
                ButtonOne.PasteFace()
            End With
    
            Try
                AddHandler ButtonOne.Click, AddressOf ButtonClick
            Catch ex As Exception
                writelog("Error Occured in preview windows while adding a Handler, Error: " & ex.Message)
                MessageBox.Show("Application Error, Please Check Error Log", strCaption, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    
    Catch ex As Exception
        writelog(ex.Message)
        writelog(ex.StackTrace)
    End Try



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 5, 2014 8:56 AM
    Moderator