![]() |
#1
|
|||
|
|||
![]()
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 |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA to immediately change the colour of a cell depending on the code placed in anothe | Phil Payne | Excel Programming | 2 | 07-27-2013 11:04 PM |
Mail Merge Code (Default Display) | ochiha_ita | Mail Merge | 3 | 04-22-2013 04:04 AM |
![]() |
Kizzy | Mail Merge | 1 | 08-13-2012 10:50 AM |
Mail merge Field Code Manipulation | macjnr | Mail Merge | 0 | 09-10-2009 11:37 AM |
![]() |
leroytrolley | Excel | 2 | 12-05-2008 02:05 AM |