Word VBA: -- Fire event on leaving a table's cell -- LostFocus? Keypress? AutoText?

Posted on 2003-03-13
Medium Priority
Last Modified: 2008-02-01
Something for true VBA gurus:

I have a table in my Word document with various data in it:
         Col1                 Col2
         NAME:                Joe Shmoe
         FILE NUMBER:         123456789
         DATE:                March 13, 2003

The user fills in the data in the table.  What my boss wants (i.e....demands) is that if the user plugs in a date such as 03/13/2003 then the computer will automatically standarize it as March 13, 2003.  In addition, if file number (SSN) is entered as 123-45-6789, I need it to change to 123456789.  I have written macros that do this but I need to find out how i should fire these.

Ideally, Word should fire it automatically after the user moves away from that cell (like a lostfocus kinda thing).  If it isn't possible, my alternatives are to execute it on save (w/ a notification to the user), execute it on print, have a "Validate" button, etc.   Also, I am trying to avoid putting a textbox object on the form.  The end-users are not the brightest when it comes to Word/PCs.

Another idea I had was to fool around with AutoText - which begs the question of how AutoText works.  Word constantly checks what we type...but how...just curious.
Question by:afzal1299
  • 2
LVL 43

Accepted Solution

Steve Knight earned 225 total points
ID: 8131737
I know you say you don't want text boxes on the page but if this is a pre-prepared document for filling by users then I would generally use a form field...

You can set some formatting (e.g. the date) and/or use entry/exit macros to take the text and update it and if you want the rest of the document (fairly) unprotected you can just protect the table say using a section break before and after it then just protect the one section.


Insert | Break | Section Berak (continous) just before and after table.
Tools | Protect | Forms | Sections button and uncheck all but the section containing the table (shows as Sec # in Status Bar).

This snippet of VBA will check a form field which has a bookmark of "SSN" and strip out all but numeric characters for instance:

hth a little


Sub CheckSSN()

Dim textin As String, textout As String
Dim c As Integer

textin = ThisDocument.FormFields("SSN").Result
textout = ""
For c = 1 To Len(textin)
    If InStr("0123456789", Mid$(textin, c, 1)) > 0 Then
        textout = textout & Mid$(textin, c, 1)
    End If

ThisDocument.FormFields("SSN").Result = textout

End Sub

Author Comment

ID: 8161209
I ended up using a form field with the section protection you mentioned.  I didn't know that separate sections could be protected.  Thanks!

I still wish there was a lost_focus on a Word cell.  Oh well!

Take care

LVL 43

Expert Comment

by:Steve Knight
ID: 8161351
Glad to help... "B" grade means "bad" rather than "good" around here though, the general idea is that you give "a"'s unless unhappy... might want to read this for future Q's:

"How do I know what grade to give?":


Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question