beginner145
09-12-2017, 01:46 PM
Hi,
I currently have a mail merge template set up with a macro that will merge and shade relevant parts of a table once the information from a csv file is merged into it.
However, there are a couple of improvements which I would like to make to it, but I have no idea of how to code it into the existing coding.
If I attach the data file and run the macro, it will merge the entire file into one document. To create a file for each polling district, What I end up doing is filtering in the Mail Merge Recipients options and selecting an individual polling district, then running the macro and using Save As to manually save the merged document.
To save me having to do this, I would like to be able to attach the data source and have the macro merge and create a document for each polling district and have it save the file with the filename of the polling district. It may also help to prompt for the macro to ask where to save the documents produced (the location would change each year and it would save editing the macro each time).
Is any of this at all possible? :-)
The macro is currently:
Sub TableShader()
Application.ScreenUpdating = False
Dim Tbl As Table, oCell As Cell, bShd As Boolean, Rng As Range, StrTxt As String
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
For Each Tbl In ActiveDocument.Tables
StrTxt = "": bShd = False
With Tbl.Range
For Each oCell In .Cells
With oCell
If .ColumnIndex = 1 Then
Set Rng = .Range
With Rng
.End = .End - 1
.Start = .Words.Last.Start
End With
If Rng.Text = StrTxt Then
bShd = True
Else
bShd = False
StrTxt = Rng.Text
End If
End If
If Not IsNumeric(Rng.Text) Then bShd = False
If bShd = True Then
If .ColumnIndex = 2 Then
.Shading.BackgroundPatternColor = RGB(255, 234, 218)
On Error Resume Next
Tbl.Cell(.RowIndex - 2, .ColumnIndex).Shading.BackgroundPatternColor = RGB(255, 234, 218)
On Error GoTo 0
End If
End If
If .ColumnIndex = 3 Then
Set Rng = .Range
With Rng
.End = .End - 1
End With
Select Case Rng.Text
Case "HEF": .Shading.BackgroundPatternColor = RGB(235, 241, 222)
Case "ITR": .Shading.BackgroundPatternColor = RGB(230, 224, 236)
Case "ITR-NR": .Shading.BackgroundPatternColor = RGB(230, 224, 236)
End Select
End If
End With
Next
End With
Next
Application.ScreenUpdating = True
End Sub
I currently have a mail merge template set up with a macro that will merge and shade relevant parts of a table once the information from a csv file is merged into it.
However, there are a couple of improvements which I would like to make to it, but I have no idea of how to code it into the existing coding.
If I attach the data file and run the macro, it will merge the entire file into one document. To create a file for each polling district, What I end up doing is filtering in the Mail Merge Recipients options and selecting an individual polling district, then running the macro and using Save As to manually save the merged document.
To save me having to do this, I would like to be able to attach the data source and have the macro merge and create a document for each polling district and have it save the file with the filename of the polling district. It may also help to prompt for the macro to ask where to save the documents produced (the location would change each year and it would save editing the macro each time).
Is any of this at all possible? :-)
The macro is currently:
Sub TableShader()
Application.ScreenUpdating = False
Dim Tbl As Table, oCell As Cell, bShd As Boolean, Rng As Range, StrTxt As String
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
For Each Tbl In ActiveDocument.Tables
StrTxt = "": bShd = False
With Tbl.Range
For Each oCell In .Cells
With oCell
If .ColumnIndex = 1 Then
Set Rng = .Range
With Rng
.End = .End - 1
.Start = .Words.Last.Start
End With
If Rng.Text = StrTxt Then
bShd = True
Else
bShd = False
StrTxt = Rng.Text
End If
End If
If Not IsNumeric(Rng.Text) Then bShd = False
If bShd = True Then
If .ColumnIndex = 2 Then
.Shading.BackgroundPatternColor = RGB(255, 234, 218)
On Error Resume Next
Tbl.Cell(.RowIndex - 2, .ColumnIndex).Shading.BackgroundPatternColor = RGB(255, 234, 218)
On Error GoTo 0
End If
End If
If .ColumnIndex = 3 Then
Set Rng = .Range
With Rng
.End = .End - 1
End With
Select Case Rng.Text
Case "HEF": .Shading.BackgroundPatternColor = RGB(235, 241, 222)
Case "ITR": .Shading.BackgroundPatternColor = RGB(230, 224, 236)
Case "ITR-NR": .Shading.BackgroundPatternColor = RGB(230, 224, 236)
End Select
End If
End With
Next
End With
Next
Application.ScreenUpdating = True
End Sub