Freigeben über


Adding Event Code to Microsoft Office Backstage View Customizations

In my last post, I introduced the basic recipe of how to customize the Microsoft Office Backstage view. I showed how to set up the XML and where to place it. Now, we'll take a look at how to add event code so that when a user interacts with your custom layout in the Backstage view, custom code fires.

First, let's look at the same basic Backstage view as last time with a few modifications. I've updated it to be a little more meaningful (the scenario and so forth here are fictional and meant only to convey technical concepts). Imagine we work at a law firm, and we need to do a custom legal scan of the documents before they go out the door. Imagine that this is a somewhat complex process that people used to do by hand. They'd manually search through the document for certain keywords or for missing sections of information. Now, we automate all of that with our own home-spun code, and we let users kick off the legal scan right from the Backstage view. Also, they can check on the progress of other scans. A final touch is that our little group of users often like to be reminded when they have not completed a document. So, we'll automate adding an Outlook task that reminds them to finish the document within three days.

Here's what the more meaningful Backstage customization looks like:

Now, I've altered the code so that two of our buttons call back to custom procedure code that does the work of automating the scanning process or creating the Outlook task for the user. Here's what the XML layout definition for our Backstage view looks like:

<customUI xmlns="https://schemas.microsoft.com/office/2009/07/customui">

<backstage>

<button id="CustomButton" label="Create Task" imageMso="AccessListTasks"

onAction="CreateOutlookTask"/>

<tab id="LegalNet" label="LegalNet">

<firstColumn>

<taskFormGroup id="LegalScan">

     <category id="ButtonCategoryAvailableFileTypes" label="LegalScan">

<task id="ScanDocument" label="Scan Document" imageMso="LookUp" >

<group id="ScanView" label="Scan View">

<primaryItem>

<menu id="viewItems" label="Check Scans" imageMso="MailMergeResultsPreview">

<menuGroup id="ItemsInProcess" itemSize="large">

<button id="ItemsButton" label="Button" description="Items in Process"

imageMso="HappyFace" onAction="ScanDocument"/>

</menuGroup>

</menu>

</primaryItem>

</group>

</task>

</category>

</taskFormGroup>

</firstColumn>

</tab>

</backstage>

</customUI>

Notice the two new areas (highlighted for you to see more easily). We've added two onAction arguments. These allow you to specify the name of the code procedure you want the application to run when the event fires. Here, we are calling two different procedures. The first is for the button that creates the Outlook task. The second is for the button that reports on document scanning results.

Here is the VBA code for these two procedures:

Sub CreateOutlookTask(ByVal control As IRibbonControl)

Dim olApp As Outlook.Application

Dim olTask As Outlook.TaskItem

olApp = New Outlook.Application

olTask = olApp.CreateItem(olTaskItem)

olTask.Subject = "Complete " + control.Context + " Document"

olTask.DueDate = DateAdd("d", 3, Now)

olTask.Body = ActiveDocument.FullName

olTask.Save()

olTask = Nothing

olApp = Nothing

End Sub

Sub ScanDocument(ByVal control As IRibbonControl)

'Perform some specific operation that does a custom scan of the document

'This scan is different from the normal "document inspection" that is built into Office

MsgBox("Scan Complete", vbInformation, "Scan Result")

End Sub

Remember that the names of the procedures in your XML have to precisely match those in the custom procedure definitions. If they are not the same, they will fail. Also, keep in mind that you need to change your document type to a .dotm or .docm for the VBA code to be enabled.

Here's what the final task looks like in Outlook:

And, here's the result that the other procedure yields to the user:

In the next post, we'll go a little further with the Backstage view and work in Visual Studio.

Rock Thought of the Day: "I Cut Like a Buffalo" by the Dead Weather

Jack White cannot be contained. He's one of the few true rock-and-rollers out there. He's willing to tear down anything he constructs, and he has such a sense of play with his image, his muse, and his relationship to his audience. He's, of course, the primary creative force behind The White Stripes. But, he's also the creative force behind The Raconteurs. And, he's the primary creative force behind the recently assembled The Dead Weatherwith alumni from The Raconteurs. The music from all three bands is different yet clearly with Jack's inventive style recognizable throughout. He works with Alicia Keys as easily as The Coal Miner's Daughter. He's one of the few artists we'll be revering à la Johnny Cash decades from now. I was in the hotel in Vegas and saw the video for "I Cut Like A Buffalo", a scrap of melodic, lyrical, and visual cunning. The tune has so many acute angles, and the notes seem to lean without ever falling down. The words are cryptic and slightly noir without being too esoteric or nihilistic. Jack strikes the right balance every time, but the balance is not about equilibrium. It's about keeping us guessing, keeping us light on our feet. Jack's music is the equivalent of a perpetually delayed forward fall—like walking to a beat. What will he do next? I can't wait to hear and see.

Rock On