0

Good morning,

I am trying to create a dynamic ribbon control showing the list of open workbooks. For this I was inspired by the code allowing to display the list of sheets of a workbook, which works. However, after multiple attempts, my control opens to a list that remains blank.

'======= Xml Code.

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
        <tabs>
            <tab id="customTab" label="FecXtools" insertBeforeMso="TabHome">
                <group id="Navig" label="Navig">
 <dynamicMenu id="ListeDynamiqueWb"
                    label="Liste Classeurs"
                    getContent="CreationMenuDynamiqueWb"
                    invalidateContentOnDrop="true"
                    size="normal"
                    imageMso="ChartShowData"/>
  </group>
</tab>
        </tabs>
    </ribbon>
</customUI>

'========== 'Vba Code

Option Explicit

'Callback for ListeDynamique getContent
'Procédure pour construire le menu dynamique
Public Sub CreationMenuDynamiqueWB(ctl As IRibbonControl, ByRef content)
    'ouverture de la balise menu
    content = "<menu xmlns=""http://schemas.microsoft.com/office/2006/01/customui"">"
    'liste les classeurs ouverts
    content = content & Liste_WB(ActiveWorkbook)
    'fermeture de la balise
    content = content & "</menu>"
End Sub



Private Function Liste_WB(wb As Workbook) As String
    Dim strTemp As String
    'Dim wb As Workbook
    
    ' Insertion d'un titre de menu
    strTemp = "<menuSeparator id=""Classeurs"" title=""Classeurs""/>"

    ' ajoute un bouton dans le menu pour chaque feuille du classeur
    For Each wb In Application.Workbooks
        strTemp = strTemp & _
            "<button " & _
            CreationAttribut("id", "Bt" & wb.Name) & " " & _
            CreationAttribut("label", wb.Name) & " " & _
            CreationAttribut("tag", wb.Name) & " " & _
            CreationAttribut("onAction", "ActivationWB") & "/>"
    Next
    Liste_WB = strTemp
End Function



Sub activateWB(control As IRibbonControl)
    wb(control.Tag).activate
End Sub

=====

I would be very grateful if you could help me figure out what is wrong.

Many thanks in advance.

Philippe Muniesa

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
phmauber
  • 11
  • 4

2 Answers2

0

activateWB()

To activate the workbook selected from the dynamic menu, use the Activate method of the Workbook object...

Sub activateWB(control As IRibbonControl)
    Workbooks(control.Tag).Activate
End Sub

Inclusion of CreationAttribut()

If you haven't already done so, make sure that you include CreationAttribut()...

Function CreationAttribut(strAttribut As String, Donnee As String) As String
    CreationAttribut = strAttribut & "=" & Chr(34) & Donnee & Chr(34)
End Function

Liste_WB()

First, there's no need to pass the active workbook to Liste_WB. So we can get rid of the parameter in its signature. Secondly, you've mis-spelled the macro being assigned to your buttons. It should be activateWB, not activationWB.

Private Function Liste_WB() As String
    Dim strTemp As String
    Dim wb As Workbook

    ' Insertion d'un titre de menu
    strTemp = "<menuSeparator id=""Classeurs"" title=""Classeurs""/>"

    ' ajoute un bouton dans le menu pour chaque feuille du classeur
    For Each wb In Application.Workbooks
        strTemp = strTemp & _
            "<button " & _
            CreationAttribut("id", "Bt" & wb.Name) & " " & _
            CreationAttribut("label", wb.Name) & " " & _
            CreationAttribut("tag", wb.Name) & " " & _
            CreationAttribut("onAction", "activateWB") & "/>"
    Next
    Liste_WB = strTemp
End Function

If you want to exclude the workbook running the code from the menu, try the following instead...

Private Function Liste_WB() As String
    Dim strTemp As String
    Dim wb As Workbook

    ' Insertion d'un titre de menu
    strTemp = "<menuSeparator id=""Classeurs"" title=""Classeurs""/>"

    ' ajoute un bouton dans le menu pour chaque feuille du classeur
    For Each wb In Application.Workbooks
        If wb.Name <> ThisWorkbook.Name Then
            strTemp = strTemp & _
                "<button " & _
                CreationAttribut("id", "Bt" & wb.Name) & " " & _
                CreationAttribut("label", wb.Name) & " " & _
                CreationAttribut("tag", wb.Name) & " " & _
                CreationAttribut("onAction", "activateWB") & "/>"
        End If
    Next
    Liste_WB = strTemp
End Function

VBA Callback for CreationMenuDynamiqueWB()

Call Liste_WB() without passing an argument...

'Callback for ListeDynamique getContent
'Procédure pour construire le menu dynamique
Public Sub CreationMenuDynamiqueWB(ctl As IRibbonControl, ByRef content)
    'ouverture de la balise menu
    content = "<menu xmlns=""http://schemas.microsoft.com/office/2006/01/customui"">"
    'liste les classeurs ouverts
    content = content & Liste_WB()
    'fermeture de la balise
    content = content & "</menu>"
End Sub
Domenic
  • 7,844
  • 2
  • 9
  • 17
  • Hello and thank you for your interest in this question. I tried the suggested edits, but unfortunately my dynamic list still remains blank. I reproduce below the modified VBA code. Thanks in advance – phmauber May 13 '23 at 07:22
  • Did you get an error? If so, which one, and on which line? – Domenic May 13 '23 at 13:58
  • Did you include the code for `CreationAttribut()`? I have edited my post to include it for completeness. – Domenic May 13 '23 at 14:00
  • Thanks for the interest in this question. I took into account all of your remarks, but nothing helped, the list opens but remains blank. I'm going to simplify the workbook to include only the code that doesn't work, and post the workbook as an attachment. – phmauber May 13 '23 at 18:17
  • to answer domene. - No I have no error, just an empty list. -2 yes I have integrated a function omitted in my first post. Function CreationAttribute(strAttribute As String, Data As String) As String CreationAttribute = strAttribute & "=" & Chr(34) & Data & Chr(34) End Function – phmauber May 13 '23 at 18:20
0

Finally, I made 2 workbooks.

The first one that doesn't work, redacting unnecessary code that doesn't concern the Ribbon, and it still doesn't work.

The second, by copying part of the Xmsl code from the ribbon, and the VBA code identical to workbook 1, and in the second workbook, it works normally.

So I guess it's coming from the XLSM code, but I'm not sure what's going on

I'd like to post both workbooks, but I don't see that option on the stackoverflow site.

I put the two workbooks on dropbox.

https://www.dropbox.com/sh/ienihcrlzqw1cda/AADmKcIXGDUjFJhL0-vTessAa?dl=0

phmauber
  • 11
  • 4
  • as I did not understand the nuance between the two workbooks, I re-imported the set of VBA modules into the workbook which displayed the list of workbooks correctly. As a result, I got the same workbook as initially (same xmls code, same VBA code), and at this SURPRISE moment, everything works normally. So I assume that the problem came from the Excel file which must have had a defect. So my problem is solved. – phmauber May 13 '23 at 19:48
  • I just finished looking at it, and the problem lies with 3 lines of XML code - lines 60, 61, and 62. Once you remove them, it works fine. I haven't checked into it further, but maybe the id's are incorrect. – Domenic May 13 '23 at 19:56
  • By the way, you would have received errors if you had enabled the option "Show add-in user interface errors", which can be found at `File >> Options >> Advanced >> General`. – Domenic May 13 '23 at 19:59
  • thank you very much it works. subsidiary question do you know a complete documentation with examples on the ribbon and the xmls codes – phmauber May 14 '23 at 06:22
  • I'm not sure how it compares with other books, but the only one I can recommend is [Excel 2007 Power Programming with VBA](http://www.amazon.com/gp/product/0470044012?ie=UTF8&tag=peltiertechni-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0470044012) by John Walkenbach. And I'm not sure how it compares with its latest edition. – Domenic May 14 '23 at 14:04
  • By the way, if you don't already have them, here are a couple of tools to help with writing XML code for the Ribbon: 1) [Office RibbonX Editor](https://github.com/fernandreu/office-ribbonx-editor) 2) [RibbonX Visual Designer](https://www.andypope.info/vba/ribboneditor_2010.htm) – Domenic May 14 '23 at 14:10
  • Domenic, I configured as you suggested me the error messages of the user interface. About softwares, I already had the office ribbonX editor, but I just downloaded the ribbonX visual Designer, which seems more user-friendly. then I bought the 2013 version of John Walkenbach which seems the last published. With that I think I'll be able to get by. Thanks again for your help – phmauber May 14 '23 at 15:44
  • That's great, it looks like you're all set, cheers! :-) – Domenic May 14 '23 at 17:32
  • Re Goodnight. I have the problem again, and I think I understand where it comes from. When there is a space in the name of sheets or in the name of workbooks, Excel throws an error. I tried to modify the vba code, but the error occurs before. Obviously it is therefore in the XLSM code that it is necessary to act.. But the question is how?? – phmauber May 14 '23 at 18:40
  • in fact it is quite possible to correct by VBA and I ended up finding the solution by deleting the spaces in the sheet or file names used for the creation of the dynamic buttons on the list of sheets or workbooks. I added a variable Btname as string and initiated it with a line Btname = Replace(wb.Name, " ", ""), finally in the content creation line the menu lines, I replaced wb.name (or ws.name) by Btname and now it 's ok – phmauber May 14 '23 at 19:21
  • Yeah, as you've discovered, the id cannot contain spaces. – Domenic May 14 '23 at 19:53
  • If you simply replace `CreationAttribut("id", "Bt" & wb.Name) & " " & _` with `CreationAttribut("id", "Bt" & Replace(wb.Name, " ", "_")) & " " & _`, your workbook name can contain a space(s). – Domenic May 14 '23 at 19:57
  • top looking forward to a next exchange – phmauber May 15 '23 at 18:11