Conditional format a table cell (background colour)

P

Paul Brown

Hello all,

Very little experience with Macros but I have created a project plan
template containing various form fields and any number of macros (mainly via
the recorder with a bit of code tweaking). One of the text fields in the
table is a due date. Via VBA is there any way to highlight the date cell or
even the entire row by changing the background colour red when the date is
overdue? I guess this would be quite straight forward in Excel via
Conditional formating but I would really like to do it in Word.

Any help appreciated.

Thanks.

Paul.
 
D

Doug Robbins

Run a macro containing the following code (modified as necesssary for the
bookmark name assigned to the formfield) on exit from the formfield into
which the date is entered:

With ActiveDocument
.Unprotect
If .FormFields("Text1").result < Now Then
.FormFields("Text1").Range.Cells(1).Shading.BackgroundPatternColor =
wdColorRed
Else
.FormFields("Text1").Range.Cells(1).Shading.BackgroundPatternColor =
wdColorAutomatic
End If
.Protect wdAllowOnlyFormFields, NoReset
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

Paul Brown

Doug Robbins said:
Run a macro containing the following code (modified as necesssary for the
bookmark name assigned to the formfield) on exit from the formfield into
which the date is entered:

With ActiveDocument
.Unprotect
If .FormFields("Text1").result < Now Then
.FormFields("Text1").Range.Cells(1).Shading.BackgroundPatternColor
= wdColorRed
Else
.FormFields("Text1").Range.Cells(1).Shading.BackgroundPatternColor
= wdColorAutomatic
End If
.Protect wdAllowOnlyFormFields, NoReset
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Hi Doug,

Thanks for the response.

I get a compile error - expected expression? These 2 lines seem to be
causing the problem but could be the way I pasted it:

..FormFields("Text1").Range.Cells(1).Shading.BackgroundPatternColor =
.FormFields("Text1").Range.Cells(1).Shading.BackgroundPatternColor =

Can you help further?
 
P

Paul Brown

The only problem is that whatever date I put in the cell (either future or
past) it formats the cell red. Have I missed something? Sorry to keep
troubling you with this.

Paul.
 

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