LParks
LParks

Reputation: 11

VBA event handler for checking cells in a table in Word

I have a table in Word: Table Example

The table is bookmarked in the document, as it can appear in different places in the document. I access the table using the bookmark like this:

Set Tbl = ActiveDocument.Bookmarks("bookmarkname").Range.Tables(1)

I have a script that checks if a cell has a checkmark and displays a Msgbox when it has a checkmark and shouldn’t, based on certain criteria of the Row and Column name.

Here’s the question:

I would like this script to fire by a Cell_OnLeave type of event, so that when user leaves a cell, script will run. Is this possible?

If this is not possible, I would like the script to fire when user leaves the table, and script can check the whole table? Maybe a bookmark_deselected event would work for that? How could this be done?

Upvotes: 1

Views: 818

Answers (2)

Victor Colomb
Victor Colomb

Reputation: 348

It is possible to create custom events for table cells in Word. Since the code is quite long, I'll just link to an example document and a GitHub gist.


I tried to comment the code as much as possible to make it understandable.

The module creates on enter, on change and on exit events for table cells and an event manager to define each event's behavior.

If you don't want to read everything, use the module by modifying the CellEventManager subroutine.
The sub receives the event type and the cell that triggered the event, so you can properly define event responses to your likings.

I also implemented a Cancel functionality for the OnExit event: Set CellEventManager = Fail during the handling of a OnExit event to prevent the selection from leaving the cell.

Upvotes: 1

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

Only the built-in events can be handled in Word. There are no specific events for either tables or bookmarks. The only event that gets you anywhere close is the WindowSelectionChange event which fires each time the selection is changed, i.e. each time the insertion point is moved.

Upvotes: 0

Related Questions