none
VSTO for Office 2007 - Buttons in a dropdown list RRS feed

  • Question

  • I've made a series of buttons on my ribbon. I would like to consolidate them into a single dropdown list. I was looking at the toolbox for office objects and added the dropdown menu object. I was able to add to the collection some button entries, lets say 1-4. however, when double click on the dropdown button, in designer, I only see the code for the button being in the ribbon. where is/do I put the button code that's a sub of the dropdown button?
    Saturday, February 9, 2013 10:55 PM

Answers

  • If you look at the events for the Ribbon Designer Dropdown you'll see there's a Selection_Changed and Button_Click event. Selection_Changed triggers for any Items you've defined in that "Collection". Button_Click triggers for any Buttons you've added.

    Items will appear at the top of the dropdown list; buttons below the items. The Items list is dynamic: you can change it during runtime; the buttons list is static, once the Ribbon has been loaded. Only "Items" will appear in the dropdown's textbox; buttons execute actions.

    The code for the two collections would be handled pretty much in the same way. The first of the snippets below shows how to branch depending on which control is selected. You can use Id or Tag to identify a button/item.

        Private Sub DropDown1_ButtonClick(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles DropDown1.ButtonClick
            Dim c As Office.IRibbonControl = e.Control
            Select Case c.Id
                Case "btnSel1"
                    MsgBox("Button 1 was clicked")
                Case "btnSel2"
                    MsgBox("Button 2 was clicked")
                Case Else
                    MsgBox("Unknown button was clicked")
            End Select
        End Sub
    
        Private Sub DropDown1_SelectionChanged(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles DropDown1.SelectionChanged
            MsgBox(DropDown1.SelectedItem.Id)
        End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by J_Furmanek Monday, February 11, 2013 11:31 PM
    Sunday, February 10, 2013 8:46 AM
    Moderator

All replies

  • If you look at the events for the Ribbon Designer Dropdown you'll see there's a Selection_Changed and Button_Click event. Selection_Changed triggers for any Items you've defined in that "Collection". Button_Click triggers for any Buttons you've added.

    Items will appear at the top of the dropdown list; buttons below the items. The Items list is dynamic: you can change it during runtime; the buttons list is static, once the Ribbon has been loaded. Only "Items" will appear in the dropdown's textbox; buttons execute actions.

    The code for the two collections would be handled pretty much in the same way. The first of the snippets below shows how to branch depending on which control is selected. You can use Id or Tag to identify a button/item.

        Private Sub DropDown1_ButtonClick(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles DropDown1.ButtonClick
            Dim c As Office.IRibbonControl = e.Control
            Select Case c.Id
                Case "btnSel1"
                    MsgBox("Button 1 was clicked")
                Case "btnSel2"
                    MsgBox("Button 2 was clicked")
                Case Else
                    MsgBox("Unknown button was clicked")
            End Select
        End Sub
    
        Private Sub DropDown1_SelectionChanged(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles DropDown1.SelectionChanged
            MsgBox(DropDown1.SelectedItem.Id)
        End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by J_Furmanek Monday, February 11, 2013 11:31 PM
    Sunday, February 10, 2013 8:46 AM
    Moderator
  • Cool, I'm trying that in my test now. For the life of me I couldn't get it to start the snippet through the methods I've been using so far which has been to just double click the object in designer. Now to see if I've been learning anything there last 2 weeks.

    Sunday, February 10, 2013 10:10 PM
  • Apparently I've not learned enough yet :) I dropped my insert logo code replacing the msgbox call from the example. fixed the ID tag's etc. but I'm getting errors "Implicit conversion from 'Object' to 'Microsoft.Office.Interop.Word.Range' in copying the value of 'ByRef' parameter 'Range' back to the matching argument."

    However in a related question, can I group the common "Dim" statements from all my buttons and place it before my initial Case "Button1" line? I like to keep my code clean looking to make any updates in the future easier.


        Private Sub DropDown1_ButtonClick(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles DropDown1.ButtonClick
            Dim c As Office.IRibbonControl = e.Control
            Select Case c.Id
                Case "Button1"
                    Dim instance As Word.InlineShapes
                    Dim FileName As String = "C:\Logos\Logo_08pt.png"
                    Dim LinkToFile As Object = False
                    Dim SaveWithDocument As Object = True
                    Dim rng As Word.Range = Globals.ThisAddIn.Application.Selection.Range
                    Dim returnValue As Word.InlineShape
                    Dim rngIls As Word.Range

                    instance = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes
                    returnValue = instance.AddPicture(FileName, _
                      LinkToFile, SaveWithDocument, rng)
                    rngIls = returnValue.Range
                    rngIls.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                    rngIls.Select()

                Case "Button2"

    Sunday, February 10, 2013 10:49 PM
  • I need to know exactly which lines of code are triggering exactly which error(s)?

    Do you have an Imports statement for Word (Imports Word = Microsoft.Office.Interop.Word) at the top of the Ribbon class?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, February 11, 2013 7:08 AM
    Moderator
  • Imports Microsoft.Office.Tools.Ribbon
    Imports Word = Microsoft.Office.Interop.Word
    Public Class Ribbon1
    
        Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub DropDown1_ButtonClick(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles DropDown1.ButtonClick
            Dim c As Office.IRibbonControl = e.Control
            Select Case c.Id
                Case "Button1"
                    Dim instance As Word.InlineShapes
                    Dim FileName As String = "c:\Logos\Logo_08pt.png"
                    Dim LinkToFile As Object = False
                    Dim SaveWithDocument As Object = True
                    Dim rng As Word.Range = Globals.ThisAddIn.Application.Selection.Range
                    Dim returnValue As Word.InlineShape
                    Dim rngIls As Word.Range
    
                    instance = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes
                    returnValue = instance.AddPicture(FileName, _
                      LinkToFile, SaveWithDocument, rng)
                    rngIls = returnValue.Range
                    rngIls.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                    rngIls.Select()
                Case "Button2"
                    Dim instance As Word.InlineShapes
                    Dim FileName As String = "c:\Logos\Logo_18pt.png"
                    Dim LinkToFile As Object = False
                    Dim SaveWithDocument As Object = True
                    Dim rng As Word.Range = Globals.ThisAddIn.Application.Selection.Range
                    Dim returnValue As Word.InlineShape
                    Dim rngIls As Word.Range
    
                    instance = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes
                    returnValue = instance.AddPicture(FileName, _
                      LinkToFile, SaveWithDocument, rng)
                    rngIls = returnValue.Range
                    rngIls.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                    rngIls.Select()
                Case "Button3"
                    Dim instance As Word.InlineShapes
                    Dim FileName As String = "c:\Logos\Logo_28pt.png"
                    Dim LinkToFile As Object = False
                    Dim SaveWithDocument As Object = True
                    Dim rng As Word.Range = Globals.ThisAddIn.Application.Selection.Range
                    Dim returnValue As Word.InlineShape
                    Dim rngIls As Word.Range
    
                    instance = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes
                    returnValue = instance.AddPicture(FileName, _
                      LinkToFile, SaveWithDocument, rng)
                    rngIls = returnValue.Range
                    rngIls.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                    rngIls.Select()
            End Select
        End Sub
    
        Private Sub DropDown1_SelectionChanged(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles DropDown1.SelectionChanged
    
        End Sub
    End Class
    

    i do now, but the errors are still these. Lines 23,38,&53 on column 49 which looks to be the 2nd line of my returnvalue call where I specify range.

    Warning    1    Implicit conversion from 'Object' to 'Microsoft.Office.Interop.Word.Range' in copying the value of 'ByRef' parameter 'Range' back to the matching argument.


    Monday, February 11, 2013 1:22 PM
  • I can't go counting the lines in the code you post...

    And a Warning isn't an error, as such. It's possible the code could still run with no problem. Does it actually not run?

    The message you're seeing would be very relevant to C# as that enforces very strict data typing. In C# all optional arguments must be declared as type Object then passed to the method "by ref". I've never heard that's necessary with VB.NET, especially if you've set Option Strict Off.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, February 11, 2013 1:52 PM
    Moderator
  • negative nothing happens. I replaced my button 3 code with
    MsgBox("Button 3 was clicked")
    to verify that I'm indeed linking the button ID's to the script and it works fine. May I should move this over to the word forum as it's now a specific word issue.
    Monday, February 11, 2013 10:24 PM
  • Ooo, I think I found the problem. I'm out of the office right now so I'm not using the same pc as i was before. Something must be configured wrong with my VM since my other code doesn't work as a stand alone so I think I do have it working.
    Monday, February 11, 2013 10:32 PM
  • yeah, i remoted into my desktop and tried out the code again and I've got all the entries i wanted added to the collection and it's working great. thanks again.
    Monday, February 11, 2013 11:35 PM