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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

764 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