none
Ribbon Control not invalidating or refreshing RRS feed

  • Question

  • Hello,

    I'm using Word 2016/365 & VBA.  I've created a ribbon in Word and my controls (dropdown) are not invalidating (refreshing) after I use them.  The drop-down is just to add points above or below a paragraph.   If I choose 24pts above the paragraph it works the first time, but the drop-down does not refresh (remains at 24pts) and if I use the macro on another paragraph and reselect 24pts again nothing happens.  If I select a different point (36pts) then it works.  I've searched everywhere in forums and have tried everything below is the code:

    XML:

    -----------------------

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customUI xmlns="http //schemas.microsoft.com/office/2009/07/customui" onLoad="RibbonControl.Onload">
      <ribbon>
        <tabs>
          <tab id="CustomTab1" insertBeforeMso="TabHome" label="QUARK" >
            <group id="grpSpacing" label="SPACING">
      <dropDown id="DDSpacing" label="Above" imageMso="ShapeUpArrow"
                          getItemCount="RibbonControl.GetDDAboveCount"
                          getItemLabel="RibbonControl.GetDDAboveLabel"
                          getSelectedItemIndex="RibbonControl.GetSelectedItemDDAboveIndex"
                          onAction="RibbonControl.myDDAbove" />
      <dropDown id="DDSpacingBelow" label="After" imageMso="ShapeDownArrow"
                          getItemCount="RibbonControl.GetDDBelowCount"
                          getItemLabel="RibbonControl.GetDDBelowLabel"
                          getSelectedItemIndex="RibbonControl.GetSelectedItemDDBelowIndex"
                          onAction="RibbonControl.myDDBelow" />
     </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>

    ----------------------------------

    VB:

    Option Explicit
    Public myRibbon As IRibbonUI
    Sub Onload(ribbon As IRibbonUI)
     
      Set myRibbon = ribbon

    End Sub
    Sub GetDDAboveCount(ByVal control As IRibbonControl, ByRef count)
     
      count = 9

    End Sub
    Sub GetDDAboveLabel(ByVal control As IRibbonControl, Index As Integer, ByRef label)
      
      label = Choose(Index + 1, "Select...", "0", "6", "12", "24", "36", "48", "60", "72")

    End Sub
    Sub GetSelectedItemDDAboveIndex(ByVal control As IRibbonControl, ByRef Index)

      Select Case control.ID
        Case Is = "Select..."
          Index = 0
        Case Else
          'Do nothing
      End Select

    End Sub
    Sub myDDAbove(ByVal control As IRibbonControl, selectedID As String, selectedIndex As Integer)
     
      Select Case selectedIndex
        Case 0
          'Do nothing
        Case 1
          modUtilities.para0pts
        Case 2
          modUtilities.para6pts
        Case 3
          modUtilities.para12pts
        Case 4
          modUtilities.Para24Pts
        Case 5
          modUtilities.para36pts
        Case 6
          modUtilities.para48pts
        Case 7
          modUtilities.para60pts
        Case 8
          modUtilities.para72pts
      End Select
     
      myRibbon.InvalidateControl control.ID

    End Sub
    Sub GetDDBelowCount(ByVal control As IRibbonControl, ByRef count)
     
      count = 5

    End Sub
    Sub GetDDBelowLabel(ByVal control As IRibbonControl, Index As Integer, ByRef label)
        
      label = Choose(Index + 1, "Select...", "0", "12", "24", "36")

    End Sub
    Sub GetSelectedItemDDBelowIndex(ByVal control As IRibbonControl, ByRef Index)
     
      Select Case control.ID
        Case Is = "Select..."
          Index = 0
        Case Else
          'Do nothing
      End Select

    End Sub
    Sub myDDBelow(ByVal control As IRibbonControl, selectedID As String, selectedIndex As Integer)
     
      Select Case selectedIndex
        Case 0
          'Do nothing
        Case 1
          modUtilities.para0ptsAfter
        Case 2
          modUtilities.para12ptsAfter
        Case 3
          modUtilities.para24ptsAfter
        Case 4
          modUtilities.para36ptsAfter
      End Select

      myRibbon.InvalidateControl control.ID

    End Sub

    ---------------------------------------

    I'd appreciate any help.  Thank you, Kyle


    • Edited by Kyle Knowles Tuesday, December 6, 2016 11:35 PM Add version of Word
    Tuesday, December 6, 2016 11:29 PM

All replies

  • Hi,

    Could you please tell me how to use the function?

    What is "modUtilities"? How should I declare it?

    I would get compile error shows variable not defined at modUtilities.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 8, 2016 6:38 AM
    Moderator
  • Hi thank you for the response.  I have a module called modUtilities with this code.  I suppose where modUtilities.para6pts is called it could just be replaced with "Selection.paragraphformat.spacebefore = X" to make it work without creating a module.

    Public Sub DoParaSpacing(ByRef iPts As Integer, ByRef iBA As Single)
    Dim Sel As Selection
    Set Sel = Selection

         If Application.Documents.count > 0 Then
              Select Case iBA
              Case 0
                   Sel.ParagraphFormat.SpaceBefore = iPts
              Case 1
                   Sel.ParagraphFormat.SpaceAfter = iPts
              End Select
         Else
              MsgBox "Please open a document."
         End If
        
    End Sub

    Sub para6pts()
         DoParaSpacing 6, 0
    End Sub

    Sub para0pts()
         DoParaSpacing 0, 0
    End Sub
    Sub para12pts()
         DoParaSpacing 12, 0
    End Sub
    Sub Para24Pts()
         DoParaSpacing 24, 0
    End Sub
    Sub para36pts()
         DoParaSpacing 36, 0
    End Sub
    Sub para48pts()
         DoParaSpacing 48, 0
    End Sub
    Sub para72pts()
         DoParaSpacing 72, 0
    End Sub
    Sub para60pts()
         DoParaSpacing 60, 0
    End Sub
    Sub para12ptsAfter()
         DoParaSpacing 12, 1
    End Sub
    Sub para24ptsAfter()
         DoParaSpacing 24, 1
    End Sub
    Sub para0ptsAfter()
         DoParaSpacing 0, 1
    End Sub
    Sub para36ptsAfter()
         DoParaSpacing 36, 1
    End Sub




    Thursday, December 8, 2016 3:51 PM
  • Hi,

    According to your description, I test as follow

    The code works perfect to set the Paragraph Spacing.

    Did you set the spaceafter when testing?

    If you set After into 24pt for the first paragraph, then set Before into 24pt for the 2nd paragraph, we could not see any change, but it is actually set successfully. You could check the paragraph properties to confirm that.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 12, 2016 2:30 AM
    Moderator
  • Hi Celeste,

    The paragraph spacing is working for me, it's the ribbon control not resetting back to "Select..." after the code runs.  In your video above the control seems to be working perfectly.  Any ideas?

    BTW, Run-time error "91" object variable or with...." is popping up periodically when attempting to invalidate the control (ribbon too).

    Thank you,

    Kyle


    • Edited by Kyle Knowles Wednesday, December 14, 2016 2:28 AM add error message
    Wednesday, December 14, 2016 1:13 AM
  • Hi,

    Which line would cause the runtime error?

    Would you get error If you test with the file: https://1drv.ms/w/s!AkcxDWH1nFmJpRCodr3UG9pYd7Xl?

    What is your specific version of Office? You could check it in the backstage view -> Account tab.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 15, 2016 1:59 AM
    Moderator
  • Hi Celeste,

    Your code worked perfectly.  What I figured out that was wrong was that I have two global templates loaded with two different ribbons and I was declaring the same ribbon as MyRibbon as ribbon in both templates so it was confusing Word.

    I do have a question - do you have any favorite books or online classes on Word specifically for VBA or any kind of advanced use of Word?  I have a couple old old books.  I understand VBA is kinda antiquated and most people use Visual Studio which I have no idea how to use.  My company won't buy visual studio anyway.  I'm completely self taught for the most part.  I did take an intermediate VBA class over 10 years ago.  I love Word and programming it especially.

    Thank you.

    Thursday, December 15, 2016 3:11 PM
  • Hi,

    It is really a good news that you found the causes and solution.

    For the question about developing with VBA or VS, I normally check MSDN documents when I have no idea about some methods or properties. Sorry that I am unable to recommend some books for you.

    In fact, developing in VS is similar to VBA because they are both Office interop by the roots, so I would still check MSDN documents about VBA when I code in VS. When I want to achieve some functions, I would firstly try to record macros in Office, and check the object and methods it uses, then code with VBA or VB.NET/C# based on different situations.

    If you are familiar with VBA, I think it is easy for you to develop in VS, you just need to try it out, then you would find they are the same, it is so easy. :)

    If you want to get started, for example, to develop an VSTO add-in, I would suggest you read articles in Create VSTO Add-ins for Office by using Visual Studio

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 16, 2016 2:56 AM
    Moderator