Consulting

Results 1 to 2 of 2

Thread: How to grey out a Ribbon Bar button

  1. #1

    How to grey out a Ribbon Bar button

    I have a template which allows me to change the caption numbering for tables and figures from the standard 1, 2 ,3, etc to that of Chapter numbering such as 1.1, 1.2, 1.3 and so on.

    Thanks to Paul Hossler, who, four years ago, answered a post and placed a very practical solution on this forum. I have taken the liberty of using his code and my template works as I want it to.

    There is one "However": If I create a new document from the template and select the button on the Ribbon Bar which says "Set Chapter Captions" it changes to the correct Chapter numbering as above and also sets a Custom Document Property (Boolean) to 'True' indicating that such an action has been taken. I can then save that document for future use with the Document Property set in it.

    When I next open that document, and as Chapter numbering has been set, I would like the Ribbon Button to be greyed out indicating that no further action is possible.

    At the moment, what happens, the saved document opens but the Ribbon Bar button appears to be active. I then click on it and the programming seems to say "Ah, yes, you have set Chapter Numbering because you have a Custom Document Property telling me so so I will turn the button to grey"

    This does happen.

    What I want the programming to do is to read that Custom Property in the document on loading and change the Ribbon Bar button from active to greyed out without any further action from me.

    This is a snip of the Ribbon Bar:

    Set Caption Headings.PNG

    Here is the template XML:

          <!--Create Chapters-->
    
                                    <button id="rxbtnConvertToChapters"
      getLabel="rxshared_getLabel"
      imageMso="FormatCellsDialog"
      size="normal"
      getEnabled="AlreadyRun"
      getScreentip="rxshared_getScreentip"
      getSupertip="rxshared_getSupertip"
      keytip="W"
      onAction="rxbtnConvertToChapters_Click" 
    
    
    In the VBA Editor, this is the code on loading the Ribbon:

    Sub rxIRibbonUI_OnLoad(ByVal ribbon As Office.IRibbonUI)
        Set grxIRibbonUI = ribbon
        'now goes back to the top of the document
        bBeenRun = False
        ActiveDocument.Bookmarks("\StartofDoc").Select
        Selection.Collapse
    End Sub
    This is the code behind the ConvertToChapters procedure:

    Sub rxbtnConvertToChapters_Click(control As IRibbonControl)
        
        If ActiveDocument.CustomDocumentProperties("Chapter Numbers") = True Then
            bBeenRun = True
            grxIRibbonUI.InvalidateControl ("rxbtnConvertToChapters")
        End If
    
    End Sub
    And here is the code for the callback "AlreadyRun" from the XML getEnabled

    'Callback for AlreadyRun
    Sub AlreadyRun(control As IRibbonControl, ByRef returnedVal)
         returnedVal = Not bBeenRun
    End Sub
    I have tried various combinations of the 'bBeenRun' setting to try and get the Convert To Chapters to go grey if Chapter Heading CustomDocumentProperty has been set to 'True'.

    Could somebody be able to point me in the right direction, please?

    Thanks

    Roderick

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,711
    Location
    Just a quick guess ...


    Sub rxIRibbonUI_OnLoad(ByVal ribbon As Office.IRibbonUI) Set grxIRibbonUI = ribbon 'now goes back to the top of the document bBeenRun =
    ActiveDocument.CustomDocumentProperties("Chapter Numbers")
    ActiveDocument.Bookmarks("\StartofDoc").Select Selection.Collapse End Sub
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •