Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

VSTO Add-In With Menu

Latest post Wed, Apr 23 2008 1:15 PM by Mattheq. 3 replies.
  • Tue, Apr 22 2008 10:32 PM

    • Mattheq
    • Top 25 Contributor
    • Joined on Wed, Apr 23 2008
    • Perth, Australia
    • Posts 15
    • Points 219

    VSTO Add-In With Menu

    Morning to all.

     I have been developing Excel code for some time, and while no expert, I can connect Excel with SQL and enforce security with Active Directory.  Now I am looking to move to VSTO, but no matter what I read, or how much time I spend developing code, I just can't seem to get my head around it.  Perhaps I shall go the way of the dinosaur ...

     So, getting down to the nuts and bolts, as it were.  I am trying to migrate a VBA add-in to VSTO.  The VBA add-in has a menu that is displayed on the main menu bar of Excel.  Can VSTO do this?  If so, can anyone please provide an 'absolute beginners' link so I can get my head around the methods used to do this in VSTO?

     Regards,

     Matthew

    Regards, Matthew

    Filed under:
    • Post Points: 21
  • Wed, Apr 23 2008 1:27 AM In reply to

    • Mattheq
    • Top 25 Contributor
    • Joined on Wed, Apr 23 2008
    • Perth, Australia
    • Posts 15
    • Points 219

    Re: VSTO Add-In With Menu

    Afternoon all.

    I found a solution to my query at MSDN.  I have edited the solution there to work in my application.  Two more cups of coffee and I would have found it this morning Tongue Tied

    Regards,
    Matthew

    Regards, Matthew

    • Post Points: 5
  • Wed, Apr 23 2008 12:46 PM In reply to

    • XL-Dennis
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Östersund, Sweden
    • Posts 38
    • Points 650

    Re: VSTO Add-In With Menu

    Hi Matthew,

    No need to apologize as VSTO is at best confusing for most Excel developers Wink

    I assume You target 2003 and not 2007. Below is all the code in the ThisAddin.VB module. I hope it will be understandable and if not please just ask. 

    Imports System.Windows.Forms

    Public Class ThisAddIn

        Private WithEvents MyButtonA As Office.CommandBarButton
        Private WithEvents MyButtonB As Office.CommandBarButton
       
        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            ' Start of VSTO generated code
            Me.Application = CType(Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(GetType(Excel.Application), Me.Application), Excel.Application)
            ' End of VSTO generated code

            Dim cbMain As Office.CommandBar = Application.CommandBars(1)
            Dim oCtl As Office.CommandBarPopup = CType(cbMain.Controls.Add(Office.MsoControlType.msoControlPopup, Temporary:=True), Office.CommandBarPopup)

            Try
                With oCtl
                    .Tag = "MyAddin"
                    .Caption = "ExcelGroup"

                    MyButtonA = CType(.Controls.Add(Type:=Office.MsoControlType.msoControlButton), Office.CommandBarButton)
                    With MyButtonA
                        .BeginGroup = True
                        .Caption = "My 1st Procedure"
                        .FaceId = 296
                        .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                    End With

                    MyButtonB = CType(.Controls.Add(Type:=Office.MsoControlType.msoControlButton), Office.CommandBarButton)
                    With MyButtonB
                        .BeginGroup = True
                        .Caption = "My 2nd Procedure"
                        .FaceId = 298
                        .Style = Office.MsoButtonStyle.msoButtonIconAndCaption
                    End With

                End With

            Catch ex As Exception
                MessageBox.Show(ex.ToString, My.Application.Info.Title.ToString)

            Finally
                oCtl = Nothing
                cbMain = Nothing
            End Try

        End Sub

        Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

            Dim oCtl As Office.CommandBarControl

            Try
                For Each oCtl In Application.CommandBars.FindControls(Tag:="MyAddin")
                    oCtl.Delete()
                    Exit For
                Next

            Catch ex As Exception
                'Skip
            Finally
                oCtl = Nothing
            End Try
        End Sub


        Private Sub MyButtonA_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles MyButtonA.Click
            MessageBox.Show("You clicked on the A button")
        End Sub

        Private Sub MyButtonB_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles MyButtonB.Click
            MessageBox.Show("You clicked on the B button")
        End Sub
    End Class

     

     

    With kind regards,
    Dennis W
    --------------------------------------------------------------
    My English Excel site My VSTO & .NET & Excel blog

    • Post Points: 21
  • Wed, Apr 23 2008 1:15 PM In reply to

    • Mattheq
    • Top 25 Contributor
    • Joined on Wed, Apr 23 2008
    • Perth, Australia
    • Posts 15
    • Points 219

    Re: VSTO Add-In With Menu

     Dennis,

    Thanks for the reply.  I was able to create the menu required, however your example has shown me how to streamline it using With ... End With blocks for CommandBarPopup sub-menus.

    I am currently developing for Excel 2003, but will be looking to upgrade to 2007 once demand for it increases.  I'm not dealing with to many commercial users of 2007 - yet! 

    Regards, Matthew

    Filed under:
    • Post Points: 5
Page 1 of 1 (4 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.