Hi,
So here's the problem that's been challenging me the last few hours. I've recently been creating a mail merged document which I had hoped to have generating different coloured headers for each mail merged page depending on a mail merged field in the header table called "Category".
The goal is to change this cell's background colour on every page depending on the text that has been mail merged into the cell. I've previously done a bit of amateur VBA using excel, but the difficulties of selecting post-mail merged header cells using bookmarks in word are defeating me.
It's rough but here is the code skeleton
Code:
Sub ColourCode()
Dim Cat1 As String
Dim Cat2 As String
Dim Cat3 As String
Dim Cat4 As String
Dim Cat5 As String
Dim X As Integer
Dim Ctext As String
Cat1 = "Category: 1 - Easy fix"
Cat2 = "Category: 2 - Instrument tubing fix"
Cat3 = "Category: 3 - Further assessment"
Cat4 = "Category: 4 - Clamp"
Cat5 = "Category: 5 - Engineering"
Documents.Open FileName:="C:\Users\me\Documents\Letters1.docm"
X = ActiveDocument.Pages.Count
For Each ActiveDocument.Tables In ActiveDocument
If ActiveDocument.Bookmarks("Category").Range.Text = Cat1 Then
Bookmarks("Category").Range.Shading.BackgroundPatternColorIndex = wdBlue
ElseIf ActiveDocument.Bookmarks("Category").Range.Text = Cat2 Then
Bookmarks("Category").Range.Shading.BackgroundPatternColorIndex = wdGreen
ElseIf ActiveDocument.Bookmarks("Category").Range.Text = Cat3 Then
Bookmarks("Category").Range.Shading.BackgroundPatternColorIndex = wdRed
ElseIf ActiveDocument.Bookmarks("Category").Range.Text = Cat4 Then
Bookmarks("Category").Range.Shading.BackgroundPatternColorIndex = wdYellow
ElseIf ActiveDocument.Bookmarks("Category").Range.Text = Cat5 Then
Bookmarks("Category").Range.Shading.BackgroundPatternColorIndex = wdPink
End If
Next
End Sub
Cheers to anyone that can help