Ribbon Customization - Closing the Currently Open Object
In a previous post, I talked about how much I enjoy developing solutions that use the new Office Fluent Ribbon UI. Along those lines, this post is the first of three upcoming posts on some specific customizations I've used in some applications recently.
The general scenario is a ribbon customization to help users work with objects in an application. This first post is pretty straight-forward where we'll create a button to close the currently open object. Start with the XML for the customization:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="tab1" label="Object Helpers">
<group id="grp1" label="Helpers">
<!-- close current object button -->
<button id="btnCloseObject" label="Close Current Object"
size="large"
imageMso="PrintPreviewClose"
onAction="OnCloseCurrentObject"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Next, create the USysRibbons table in a database to store ribbon customizations if you haven't already. You can run the following SQL statement in a new query to create the table. Since this is a data definition query, the database will need to be enabled for this to work.
CREATE TABLE USysRibbons
(
RibbonName Text(255) CONSTRAINT PrimaryKey PRIMARY KEY,
RibbonXml MEMO
);
In the USysRibbons table, add a new record and set the RibbonName field to "BlogSample1" and the RibbonXml field to the XML for the customization. Close and re-open the database and click the Object Helpers tab. You should have something that looks like this:
You'll notice that the button in the customization calls a callback in the onAction attribute called OnCloseCurrentObject. Add the following code to a new module to implement this routine. You'll need a reference to the Microsoft Office 12.0 Object Library for this code to work.
Public Sub OnCloseCurrentObject(ctl As IRibbonControl)
DoCmd.Close CurrentObjectType, CurrentObjectName
End Sub
We're using the CurrentObjectType and CurrentObjectName properties of the Application object to close the object that is currently open. To try this out, open a few different types of objects and click the button.
Rob Cooper is a Test Lead at Microsoft working on Microsoft Access. He started at Microsoft in 1998 working in Access product support in Charlotte, NC and then moved to Redmond to join the test team in 2001. For Access 2007, Rob worked on the new Grouping Pane for Reports, and security features such as Database Encryption and Disabled Mode. He is also a co-author on Expert Access 2007 Programming and Access 2007 VBA Programmer's Reference, both published by Wrox.
Rob also spends time on www.utteraccess.com reading and answering questions.