Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Repurpose User Interface Controls
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

This Microsoft Word Help & Microsoft Word Tips page will show you several methods to repurpose controls in the Word User Interface (UI).

What controls? What is repurposing? Both fair questions

Basic Demonstration

You can create your own simple example as follows:

VBA Script:
Sub FileSave()
  If ActiveDocument.Saved Then
    MsgBox "I've already been saved so you are wasting your time."
  Else
   ActiveDocument.Save
  End If
lbl_Exit:
  Exit Sub
End Sub

Site Note icon See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

In the preceding example we repurposed the control by intercepting the Word command FileSave with VBA. This is a particularly handy method that works with a lot of Word controls. For a more detailed discussion of this method see the Microsoft Word MVP FAQ: Intercepting Events like Save and Print

Advanced Demonstration

Ok, so why would anyone want or need to repurpose a control? Let's take Mr. Spacely of intergalactic Spacely Sprockets. Like many of us, he is concerned about the environment. In order to save paper he has told George and his other employees to use "green" margins of one inch or less in all Spacely Sprockets company documents. He wants his IT gurus to devise a compliance measure.

For this example we will create a Word Template Add-In that will be loaded on all Spacely Sprockets computers. We will repurpose the necessary controls by monitoring control click events.

VBA Script:
Private WithEvents ctrlEvent As CommandBarButton

Sub AutoExec()
  Set ctrlEvent = Application.CommandBars("Menu Bar").Controls("File").Controls("Print...")
lbl_Exit:
  Exit Sub:
End Sub

Private Sub ctrlEvent_Click(ByVal Ctrl As CommandBarButton, CancelDefault As Boolean)
  CancelDefault = True
  Select Case True
    Case ActiveDocument.PageSetup.RightMargin > 72
      MsgBox "Set green document margins and try again."
    Case ActiveDocument.PageSetup.LeftMargin > 72
      MsgBox "Set green document margins and try again."
    Case Else
      CancelDefault = False
  End Select
lbl_Exit:
  Exit Sub
End Sub

In the preceding example we repurposed the Print... control by monitoring the click event.

Site Note IconNotes:
     1.  Unlike intercepting commands demonstrated in the first example, click event only monitors a control click.  It does not monitor other execution methods e.g., keyboard shortcuts.

     2. For more on template add-ins and how to load them, see: Organizing Your Macros/Template Add-ins at: Installing Macros

For the final example, we will repurpose the FilePrint and FilePrintQuick controls on the Word 2007 Office Menu.

VBA Script:
Private Sub RepurposedPrint(ByVal control As IRibbonControl, ByRef cancelDefault)
  cancelDefault = True
  Select Case True
    Case ActiveDocument.PageSetup.RightMargin > 72
      MsgBox "Set green document margins and try again."
    Case ActiveDocument.PageSetup.LeftMargin > 72
      MsgBox "Set green document margins and try again."
    Case Else
      cancelDefault = False
  End Select
lbl_Exit:
  Exit Sub
End Sub
VBA Script:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
 <commands>
  <command idMso="FilePrint" onAction="RibbonControl.RepurposedPrint"/>
  <command idMso="FilePrintQuick" onAction="RibbonControl.RepurposedPrint"/>
  </commands>
</customUI>

Site Note iconNote: Unlike intercepting the click event, repurposed ribbon controls capture both the click event and keyboard shortcuts!

That's it! I hope you have found this tips page useful and informative.  You can download the template add-in which includes the RibbonXML and VBA demonstrated above here: Repurpose Print Controls.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo