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

StackOverflow https://stackoverflow.com/questions/18484290

  •  26-06-2022
  •  | 
  •  

Pregunta

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 !

¿Fue útil?

Solución

@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.

Otros consejos

The Backstage is part of the Application, and you have specified that certain macros should be called for the onShow and onHide events.

Excel will always assume that an unqualified macro, e.g., Backstage_onShow should be found within scope of the ActiveWorkbook. When this macro does not exist in the ActiveWorkbook, the error raises.

To resolve this, revise the XML so that the macro call qualifies a specific workbook:

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

Regarding Whether This Method Can Work on an XLTM File

Follow-up from comments:

When you use the above method on an XLSM file, the file which contains the Backstage_OnShow macro is open, and so calls to Exemple.xlsm!Backstage_OnShow work, because Exemple.xlsm is an open workbook file.

When you use this method on an XLTM file, to create a new file from template, the explicit XML with onShow ="Exemple.xltm!Backstage_OnShow will fail because the macro cannot be found. The macro cannot be found because Exemple.xltm is not a valid member of the Workbooks collection.

If, as you previously attempt, the XML does not fully qualify the macro (e.g., onShow="Backstage_OnShow), you will get the same error if you navigate to any open workbook which was not created by the XLTM, for the same reason: the macros cannot be found. In this case, the macro cannot be found in the scope of ActiveWorkbook.

It seems this simply may not work with an XLTM file (at least not without considerable effort)

You might be able to make it work if, instead of hijacking the Backstage you create a custom tab.

Alternatively, I think you would have to modify the Ribbon XML for each new instance of the file, and while it is possible to modify the Ribbon XML, I think it is not possible to do while the file is open.

Best solution I can think of would be to create an add-in or macro that does:

1) Prompt user for new filename 2) Create new file from template, save & close it 3) Modify the contents of the new file's Ribbon XML 4) Open the new file

You could see this for information about unpacking and modifying the XML:

http://www.jkp-ads.com/articles/Excel2007FileFormat02.asp)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top