Format table cell based on text entry

N

Newt

Is it possible to format a cell in a table as soon as a user starts entering
text? Here's what I'd like:

1. Cell starts with no text and has light yellow background
2. User begins typing, which turns the background white

Basically, I'd like something the same as conditional formatting in Excel
(embedding a spreadsheet is not an option).

Here's what I was trying, but it doesn't work:

Private Sub celltext()
If ActiveDocument.Tables(2).Cell(1, 2).Range.Text = "" Then
ActiveDocument.Tables(2).Cell(1, 2).Shading.BackgroundPatternColor =
wdColorLightYellow
Else
ActiveDocument.Tables(2).Cell(1, 2).Shading.BackgroundPatternColor =
wdColorWhite
End If
End Sub

I see two problems. One, I can't seem to get it to read the text in the cell
and two, I don't think that starting to type in a cell is a trigger to
activate the code. Any ideas?
 
M

macropod

Hi Newt,

Word doesn't have the same kind of event-driven functionality that you have in Excel, so you need to do something to cause the macro
to run. You could, for example, insert a macrobutton field to trigger the macro, or you could use a formfield to trigger the macro
and protect your document for forms.

Cheers
 
N

Newt

Thanks for the response. I guess I should consider building it in Excel.

macropod said:
Hi Newt,

Word doesn't have the same kind of event-driven functionality that you have in Excel, so you need to do something to cause the macro
to run. You could, for example, insert a macrobutton field to trigger the macro, or you could use a formfield to trigger the macro
and protect your document for forms.

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Newt said:
Is it possible to format a cell in a table as soon as a user starts entering
text? Here's what I'd like:

1. Cell starts with no text and has light yellow background
2. User begins typing, which turns the background white

Basically, I'd like something the same as conditional formatting in Excel
(embedding a spreadsheet is not an option).

Here's what I was trying, but it doesn't work:

Private Sub celltext()
If ActiveDocument.Tables(2).Cell(1, 2).Range.Text = "" Then
ActiveDocument.Tables(2).Cell(1, 2).Shading.BackgroundPatternColor =
wdColorLightYellow
Else
ActiveDocument.Tables(2).Cell(1, 2).Shading.BackgroundPatternColor =
wdColorWhite
End If
End Sub

I see two problems. One, I can't seem to get it to read the text in the cell
and two, I don't think that starting to type in a cell is a trigger to
activate the code. Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top