Resting Anchor

The Anchorage

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

Content Control Custom Events
(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 Tips & Microsoft Word Help page introduces and demonstrates a method that you can use to create "custom" content control "On Enter, OnChange and OnExit" events using VBA procedures.

While the built-in Document_ContentControlOnEnter and Document_ContentControlOnExit events have improved with the release of Word2010, there remains a persistent bug in the Word2007 OnExit event. Using this method, you can avoid that bug and any text or changes that your document users make in content controls can be evaluated and processed in real time, thus eliminating the need for the document user to first "exit" a content control.

Before going any further I want to give some credit where credit is due. Animated gif - superJeff Vandervoort first and foremost. Jeff participates in the Microsoft Office Word for Developers forum.  Without his skills and his willingness to share the fruits of those skills, this tips page would still be unpublished and gathering dust.

Ever since content controls were introduced in Word 2007 I have wondered over I dunno icon, sought after Pray icon, and frequently groused Frustration\Dismay icon about why Microsoft has failed to provide a built-in "Document_ContentControlOnChange" event. While I was wondering and grousing, Jeff devised a relatively simple VBA procedure that continually monitors the active content control's range and when any change occurs triggers a call to another procedure that can evaluate and process the change.

I would also like to mention and offer my appreciation to two regular contributors in the Microsoft Answers forum; "HansV" (Hans Vogelar) and Andreas Killer. Both assisted in this effort with solutions to several nagging problems in the code.

At the time of this writing Jeff's procedure is unpublished and part of a larger custom application that he is developing. With Jeff's concurrence I have expanded the scope of his monitoring procedure to detect and trigger custom OnEnter and OnExit events. Jeff says that his "baby sub-routine" appears "all grown up" in the solution demonstrated here. I say thank you Jeff for sharing your baby with me!!

For the discussion, I'm using a Word 2010 document containing content controls as shown in the following illustration. Each of the content controls is locked to prevent deletion. The the dependent (clone or secondary) controls are locked to prevent users from directly editing content. The content control associated with student grade is a dropdown list control with "First, Second and Third" assigned as list entries.  The checkbox control is functional in Word 2010 only.

Site Note IconNote: The illustrations in this tips page may or may not reflect that actual appearance or arrangement of the content controls contained in the demonstration document that you can download using the link at the end of this tips page.

cc_events 1

Site Note IconNote: The illustrations in this tips page may or may not reflect that actual appearance or arrangement of the content controls contained in the demonstration document that you can download using the link at the end of this tips page.

The method uses the built-in Document_Open and Document_Close events to start and stop the monitor. These events are contained in the Word "ThisDocument" class module. The code used in these events for this demonstration is shown in the code  pane below:

VBA Script:
Option Explicit
Private Sub Document_Close()
  Application.OnTime Now + TimeSerial(0, 0, 0.001), "modMain.StopMonitoring"
End Sub
Private Sub Document_Open()
  Application.OnTime Now + TimeSerial(0, 0, 0.001), "modMain.StartMonitoring"
End Sub

The demonstration document includes controls added to the QAT used to start and stop the monitor.

cc events 3

The remaining VBA variable declarations, procedures and functions are contained in two standard project modules "modMain" and "modAutoFill."  The code is too lengthy and would be of little value to publish here, but is fully commented and available in the demonstration document.

With continuous monitoring and the custom events you make your documents truly interactive with the user.  The user sees the effects of his or her actions in real time with no need to first exit a control.

cc events 4

cc events 5

cc events 6

cc events 7

cc events 8

cc events 9

cc events 10
Note: Checkbox controls are available in Word 2010 documents only.

See the remaining examples for yourself by downloading and experimenting with the demonstration documents.  I've included files for Word 2007 and Word 2010here:  Content Control Custom Events Demonstration Package

That's it! I hope you have found this tips page useful and informative. For more on repeating data, validating data, and setting a display using content controls see my tip pages on: Repeating Data, Validating Content Control Entries and Insert Content with Content 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