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)