Search code examples
vbaexcelribbonx

Excel-2010 - CustomUI - Backstage : Errors when various files are opened in the same instance


Firstly, i'm very happy to join the community. I hope we could often exchange advice. I'm french so excuse me for the mistakes in the sentences.

I try to explain my problem :

I had the "good" idea to use the Backstage of my Excel file to create a small dashboard. It works very well.

The problem arises when this file is opened at the same time as another file, in the same instance of Excel. The second file is trying to access functions "Backstage_OnShow" and "Backstage_OnHide" of my workbook so I have a message "Impossible to run the macro 'Backstage_OnShow' (or 'Backstage_OnHide'). It is possible that the macro is not available in this workbook ..." <- This is a translation to the french error message.

How can I do to not have this message or rather to ensure that the backstage is specific to my file and not the instance of Excel?

I show with my code snippets. It will be more clear.

In my XML, I have this :

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad ="Ribbon_Load">
  <ribbon startFromScratch="false"/>
  <backstage onShow="Backstage_onShow" onHide="Backstage_onHide">
...
  </backstage>
</customUI>

In my Excel file, i have this :

Public Sub Ribbon_Load(ribbon As IRibbonUI)

  Set Ruban = ribbon

End Sub

Public Sub Backstage_onShow(ByVal contextObject As Object)

    'Rafraichissement du ruban
    Ruban.Invalidate

End Sub

Public Sub Backstage_onHide(ByVal contextObject As Object)



End Sub

All this is contained in an Excel file, which is normal. In Excel 2010, Excel files open by default in the same instance, which does not bother me, but, when a "normal" file is opened in the same instance as my customized backstage file, the normal file tries, I do not know by what miracle, to access the function Backstage_onShow Backstage_onHide and as soon as I display its backstage. However, this file should not even knows these functions exist because they are not reported for him.

Thank you in advance.

Sincerely,

Patrice.

PS : this is a link if you want to show my file. It's a safe code snippets of course !


Solution

  • @David, i have a solution !!!!

    Thanks you to have take your time to answer me !!!

    I try to explain you.

    So, if we think a few moment, we note the problem is we need a stated place to store our code snippets. This place is in the XLSTART !!! If we use the PERSONAL.XLSB to store that :

    Public Sub Ribbon_Load(ribbon As IRibbonUI)
    
      Set Ruban = ribbon
    
    End Sub
    
    Public Sub Backstage_onShow(ByVal contextObject As Object)
    
        'Rafraichissement du ruban
        Ruban.Invalidate
    
    End Sub
    
    Public Sub Backstage_onHide(ByVal contextObject As Object)
    
        'ErreurSaisieTaux = 0
    
    End Sub
    

    After, we can modify the customui.xml like that :

    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad ="PERSONAL.XLSB!Ribbon_Load">
      <ribbon startFromScratch="false"/>
      <backstage onShow="PERSONAL.XLSB!Backstage_onShow" onHide="PERSONAL.XLSB!Backstage_onHide">
    ...
      </backstage>
    </customUI>
    

    I have tried and it works.

    What do you think about this idea ? Do you see drawbacks with this method ?

    Patrice.