Events not getting fired/executed in combobox in E

2019-08-31 17:47发布

I have created a ribbon in Excel VBA and below is the snapshot of the code to create a combo box and add some items. The combo box gets created successfully, however the onChange event is not getting fired when I select any item.

ribbonXML = ribbonXML + "       <mso:group  id='StyleGruop'  label='Styles'>" & vbNewLine
ribbonXML = ribbonXML + "           <mso:button  id='btnDeleteStyle'    imageMso='PictureStylesGallery' screentip='Delete Styles' size='large' onAction='About_onAction'/>" & vbNewLine
ribbonXML = ribbonXML + "           <mso:separator  id='Separator4'/>" & vbNewLine
ribbonXML = ribbonXML + "           <mso:dropDown id='ddlStyles' label='Style:' onAction='rxddSelectSheet_Click' getItemID='rxitemddSelectSheet_getItemId' getItemCount='rxitemddSelectSheet_getItemCount' getItemLabel='rxitemddSelectSheet_getItemLabel' />" & vbNewLine
'ribbonXML = ribbonXML + "           <mso:dropDown id='ddlFreqStyles' label='Frequent Style:' getItemCount='Length1ItemCount' getItemLabel='Length1ListItem' onAction='Length1OnAction' getSelectedItemIndex='Length1ItemSelectedIndex' sizeString='centimeters' />" & vbNewLine
ribbonXML = ribbonXML + "           <mso:comboBox id='MyMonth' label='Month:' showLabel='true' onChange='cbMonth_onChange' getItemID='cbMonth_getItemID' getItemCount='cbMonth_getItemCount' getItemLabel='cbMonth_getItemLabel' >" & vbNewLine
ribbonXML = ribbonXML + "               <mso:item id='Month1' label='Jan'/>" & vbNewLine
ribbonXML = ribbonXML + "               <mso:item id='Month2' label='Feb'/>" & vbNewLine
ribbonXML = ribbonXML + "               <mso:item id='Month3' label='Mar'/>" & vbNewLine
ribbonXML = ribbonXML + "           </mso:comboBox>" & vbNewLine
ribbonXML = ribbonXML + "       </mso:group>" & vbNewLine

Below are the functions:

Public Sub cbMonth_getItemID(control As IRibbonControl, index As Integer, ByRef id)
     '
     ' Code for getItemID callback. Ribbon control comboBox
     '
    returnedVal = "Month" & index

End Sub

Public Sub cbMonth_getItemCount(control As IRibbonControl, ByRef returnedVal)
     '
     ' Code for getItemCount callback. Ribbon control comboBox
     '
    returnedVal = 12
End Sub

Public Sub cbMonth_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
     '
     ' Code for getItemLabel callback. Ribbon control comboBox
     '


    returnedVal = Format(DateSerial(2011, 7 + index, 1), "mmm-yyyy") 'myMonth

End Sub

Public Sub cbMonth_onChange(control As IRibbonControl, Text As String)
     '
     ' Code for onChange callback. Ribbon control comboBox
     '
    myMonth = Text
    VBA.MsgBox myMonth
End Sub

Any help would be appreciated. Thanks in advance.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-31 18:29

Hhhmmm, sorry but I'm not fully understanding what you've done. You can't create a Ribbon with VBA. There are different rules with VSTO... but that's a different issue. I assume you are trying VBA development.

So if this is the case, it looks to me that within the ribbon XML string, which is used to build the ribbon, and define the events isn't defining the OnChange event.

So basically, again assuming this is VBA, this is what I would do.... (there's lot of tutorials on Ribbon building on the web, so I'll not define the exact specifics here..)

  1. Save your XL file as a Zip file.
  2. Save your ribbon XML, which includes the OnChanged event in the zip archive.
  3. Put your event handlers in the Excel file.
  4. Resave as a XL file.
  5. Make sure you get a reference to the Ribbon in the VBA. If you don't do this, no events will fire.
  6. Make sure you have the callback function for the OnChanged event.

If you've done steps 1 to 4 and the XML is correct, you're probably failing with steps 5. and/or 6.

Hope that helps....

查看更多
登录 后发表回答