Ribbon Customization - Closing the Currently Open Object

Published 24 September 07 09:45 AM

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:

Close object button in a Ribbon customization in Access 2007

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.

Comments

# Judith Kramer said on September 25, 2007 1:41 AM:

Nice, but how can I activate the ribbon? Is there something like tab1.SetFocus?

# Chris Sergent said on September 25, 2007 1:42 PM:

I liked this. I haven't seen very many short explanations on using VBA. I attempted to create my own, but receive a message ...can't run the macro or callback function

I am able to run the VBA code using a macro and referencing the macro in the XML. Any ideas why I might be getting this?

Your code ran fine when I referenced it.

Here is my code that I referred to in XML as "SickTime":

Public Sub SickTime()

   Dim stAppName As String

       stAppName = "C:\Program Files\Internet Explorer\iexplore.exe http://decweb01/forms/sickinj.pdf"

   Call Shell(stAppName, vbMaximizedFocus)

End Sub

# cruncher06 said on September 25, 2007 1:58 PM:

RE: Activate the Ribbon

Judith, to activate the ribbon, if you have a form open, display it's properties in design view. In the form properties, select the Other tab.

Select the ribbon name, in this case BlogSample1 and save your form.

Close the database and re-open it.

Open the form and you should see your new menu button.

# robcooper said on September 25, 2007 3:47 PM:

Chris, thanks for your feedback! Callback routines written in VBA require a specific signature. If this routine is being used from a button control in the XML, change the signature to look something like:

Public Sub SickTime(ctl As IRibbonControl)

Remember that this will require a reference to the Microsoft Office 12.0 Object Library which you can add from the dialog under Tools > References.

# Chris Sergent said on September 25, 2007 4:49 PM:

Thanks Rob. Just needed the code in parentheses and it works.

# Judith Kramer said on September 26, 2007 1:12 AM:

@cruncher06: Thanks. But I'd like to select and activate the other tab with VBA code. Is that possible?

# robcooper said on September 26, 2007 1:39 AM:

Judith - unfortunately there is no object model for the Ribbon as such so there's not really a good way to set focus to a particular tab. Contextual tabs used in a Form or Report will take focus when they are first opened, but if you switch between open objects, focus will not be given back to the contextual tab.

Rob

New Comments to this post are disabled

About robcooper

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.
Page view tracker