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

VSTO Forms - Bug or Code Error?

Latest post Wed, May 21 2008 5:20 AM by XL-Dennis. 6 replies.
  • Mon, May 19 2008 11:38 PM

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

    VSTO Forms - Bug or Code Error?

    Hello.

    Since Dennis helped with my original post I have been happily converting an existing VBA add-in to VSTO.  This has given me plenty of practice - as well as getting use to the VSTO way of doing things.

    I have come up against a problem that I can't seem to resolve, and Google hasn't helped.  My problems is:

    I have created a custom menu for Excel with the events controlled by the VSTO add-in.  One of the menus is

    Private Sub mnuItem1_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, _
    ByRef CancelDefault As Boolean) Handles mnuItem1.Click
    Dim frmMyForm1 As New MyForm1
    frmMyForm1.Show()

    End Sub

     Another menu is:

     Private Sub mnuItem2_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, _
    ByRef CancelDefault As Boolean) Handles mnuItem2.Click
    Dim frmMyForm2 As New MyForm2
    frmMyForm2.Show()

    End Sub

     When the event for mnuItem1 is triggered by the menu, two forms show - MyForm1 and MyForm2!!

    I have searched through my code, variables and triggers, and I have stepped through the code, but there is no event that should show MyForm2 when the mnuItem1 button is clicked.

    Is there somewhere else in the code I should be looking to resolve this?  It just doesn't make sense to me.

    Regards, Matthew

    Filed under:
    • Post Points: 21
  • Tue, May 20 2008 1:41 AM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 304
    • Points 5,058
    • MVP

    Re: VSTO Forms - Bug or Code Error?

     Matthew

    I can see nothing at first glance, i just wonder if there is anything going on in the classes MyForm1, MyForm2. of course we can't see those. Sometimes if you copy classes you forget to change the class signature or similar.

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under:
    • Post Points: 21
  • Tue, May 20 2008 4:49 AM In reply to

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

    Re: VSTO Forms - Bug or Code Error?

     Matthew,

    If You place a breakpoint in the code and step through the code can You see if the code does anything wrong? 

    Could You please give us which version of VSTO You use and also which Excel version You work with.

    Below is the expanded code based on the first thread where I have added the events and Windows Forms. It works as expected:

    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
            Dim frm As New frm1

            Try
                frm.Show()
            Catch ex As Exception

            End Try
        End Sub

        Private Sub MyButtonB_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles MyButtonB.Click
            Dim frm As New frm2

            Try
                frm.Show()
            Catch ex As Exception

            End Try
        End Sub

     

    End Class

     

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

    • Post Points: 21
  • Tue, May 20 2008 5:26 AM In reply to

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

    Re: VSTO Forms - Bug or Code Error?

     Hello Nick and Dennis.

    Firstly, thank you for your replies. 

    • I am using VSTO Second Edition.
    • Using the Try ... Catch ... End Try no errors reported, but 2 forms still showed.
    • I am getting the 'error' in Excel 2003 and Excel 2007
    • Stepping through the code did not highlight anywhere that two forms are called

    Private Sub mnuItem1_Click(ByVal Ctrl As  Microsoft.Office.Core.CommandBarButton, _
    ByRef CancelDefault As Boolean) Handles mnuItem1.Click

    Dim frmMyForm1 As New MyForm1

    Try
    frmMyForm1.Show()
    Catch ex As Exception
    MessageBox.Show(ex.ToString) 'No error generated here but two forms still show.
    End Try

    End Sub

    Here is the Load event for MyForm1:

    Public Class MyForm1

    Private objMyCollection As New MyCollection

    Private Sub MyForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Set DateTimePicker Date to today
    Me.dtpShift.Value = Today()

    With Me.ShiftCombo1  'Custom Control based on ComboBox
    .TimeOffset = 7 / 24  'as Double
    .Text = "Day"
    End With

    'Add items to MyCollection
    objMyCollection.Add(Me.WaterBox1) 'Custom Controls based on TextBox
    objMyCollection.Add(Me.WaterBox2) 'Used for data validation routine
    objMyCollection.Add(Me.WaterBox3)
    objMyCollection.Add(Me.WaterBox4)
    objMyCollection.Add(Me.WaterBox5)
    objMyCollection.Add(Me.WaterBox6)
    objMyCollection.Add(Me.WaterBox7)
    objMyCollection.Add(Me.WaterBox8)

    End Sub

     

     

    After the Load event finishes, I am passed to the Disassembly Information.  Not sure what this tells me Confused

    Any ideas would be welcomed.

    Regards, Matthew

    Filed under: ,
    • Post Points: 5
  • Tue, May 20 2008 10:02 PM In reply to

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

    Re: VSTO Forms - Bug or Code Error?

     Hello again.

    Just an update on what is happening with this problem - and now a solution!

    • I commented out the mnuItem1.click event, cleaned and rebuilt the solution, then debugged.  Only Form2 loaded.  The fact that Form2 loaded on an event that was commented out was curious, so I searched the entire document to ensure I had not duplicated the click event.  I hadn't, so back to the drawing board, so to speak.
    • I deleted the bin and obj directories from the solution in case there was a corrupted file - still no joy.

    The answer to the problem was in the menu creation code:

    mnuItem1 = CType(menuMyMenu.CommandBar.Controls.Add( _
                        Type:=Office.MsoControlType.msoControlButton, _
                        Temporary:=True), _
                        Office.CommandBarButton)
                    With mnuItem1
                        .Caption = "Menu Item 1"
                        .Tag = "Item"
                    End With

     

    mnuItem2 = CType(menuMyMenu.CommandBar.Controls.Add( _
                        Type:=Office.MsoControlType.msoControlButton, _
                        Temporary:=True), _
                        Office.CommandBarButton)
                    With mnuItem2
                        .Caption = "Menu Item2"
                        .Tag = "Item"
                    End With

    The Tag property for both menu items were identical.  Changing the Tag property on Item2 stopped 2 forms showing from one click event!

    I wouldn't have expected that the Tag property for the menu would have caused this behaviour, but changing this has resolved the issue.  Does anyone have any idea if this behaviour is by design, or whether it is a bug?  Of course it could just be my limited experience with VSTO Wink

    Regards, Matthew

    Filed under:
    • Post Points: 5
  • Tue, May 20 2008 10:14 PM In reply to

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

    Re: VSTO Forms - Bug or Code Error?

     Just to clarify:

    I have two machines I am developing on:

    • VSTO Second Edition with Visual Studio 2005 Professional on Vista Ultimate and developing for Excel 2007
    • VSTO Second Edition with Visual Studio 2005 Professional on XP Professional and developing for Excel 2003

    The problem with the Tag property did not occur on the XP machine running 2003, only the Vista machine running 2007.

    I tested the above solution by changing tags on a number of other menu creation code blocks and was able to reproduce the behaviour of multiple forms simultaneously loading from one click event.

    Hope this helps.

    Regards, Matthew

    Filed under:
    • Post Points: 21
  • Wed, May 21 2008 5:20 AM In reply to

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

    Re: VSTO Forms - Bug or Code Error?

     Matthew,

    >>I wouldn't have expected that the Tag property for the menu would have caused this behaviour, but changing this has resolved the issue. 

    Neither did I! I did some test with VSTO 2005 SE & Excel 07 but I was not able to reproduce it. However, since I have seen two indentical setups produce two different outputs I now longer expect anything.


    Thanks for letting us know.

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

    • Post Points: 5
Page 1 of 1 (7 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.