Hi Mary,
Step by step:
1. I assume you're working in a document (a file whose name has a .doc
extension). Use the File > Save As command and set the "Save as type"
box at the bottom of the dialog to "Document Template (*.dot)". The
folder automatically changes to your Templates folder. Save the
template there with an appropriate name.
2. Press Alt+F11 to open the macro editor. On the left side there
should be a pane with a tree view showing the open templates (if not,
click View > Project Explorer). One of the icons in the tree will be
labeled with the name of the template.
3. Expand the icon for the template (click the little plus sign next
to it if necessary). There will be a sub-item labeled Microsoft Word
Objects and, under that, one labeled ThisDocument. Double-click the
ThisDocument icon. You should get a large empty pane to the right.
4. Copy the following code (which I've altered to match the behavior
you described) and paste it into the large empty pane.
5. Save the template and close it.
6. Click File > New. If you get a task pane on the right, choose
Templates On My Computer. Find the template in the list and
double-click it to make a new document based on the template. Clicking
in any cell in the last three columns of the table will change the
background color from blank to the proper color for that column, and
clicking it again will change it back to blank.
Option Explicit
Private WithEvents wdApp As Word.Application
Private Sub Document_New()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub
Private Sub Document_Open()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub
Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
' get out as fast as possible if not in table
If Not Sel.Information(wdWithInTable) Then Exit Sub
With Sel.Cells(1)
' work only on the rightmost three columns
Select Case .ColumnIndex
Case Sel.Tables(1).Columns.Count - 2:
' green
If .Shading.BackgroundPatternColorIndex <> wdAuto Then
.Shading.BackgroundPatternColorIndex = wdAuto
Else
.Shading.BackgroundPatternColorIndex = wdBrightGreen
End If
Case Sel.Tables(1).Columns.Count - 1:
' yellow
If .Shading.BackgroundPatternColorIndex <> wdAuto Then
.Shading.BackgroundPatternColorIndex = wdAuto
Else
.Shading.BackgroundPatternColorIndex = wdYellow
End If
Case Sel.Tables(1).Columns.Count:
'red
If .Shading.BackgroundPatternColorIndex <> wdAuto Then
.Shading.BackgroundPatternColorIndex = wdAuto
Else
.Shading.BackgroundPatternColorIndex = wdRed
End If
End Select
End With
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.