PDA

View Full Version : Word



mg16
07-21-2011, 08:50 AM
Hi
I have developed an application which generates a large word document (1000+ pages). This document tables in it which I would like to manipulate. I am new to VBA, so would be grateful if anyone could help me achieve what I want to do:

After the document has been finished generating (I merge XML with Word 2003 XSL stylesheet with imbedded XML tags using MSXSL4 to generate doc) I want to do the following:

- Go through all tables and rows in the word doc
- Look for rows where there is the text "ACCNAME=XXXXX" in the second cell of the row
- Remove the ACCNAME= part of the text in the cell (cell 2)
- Merge all cells of that row and place text that was in cell 2 in the merged cell
- Make bold the text
- Shade the whole row

How would I go about this using VBA?

Many thanks in Advance

gmaxey
07-21-2011, 02:34 PM
Something like this:

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oTbl As Word.Table
Dim oRow As Word.Row
Dim oCell As Word.Cell
Dim bMerge As Boolean
Dim oRng As Word.Range
Dim oRngDup As Word.Range
For Each oTbl In ActiveDocument.Tables
For Each oRow In oTbl.Rows
bMerge = False
Set oCell = oRow.Cells(2)
Set oRng = oCell.Range
Set oRngDup = oRng.Duplicate
With oRng.Find
.Text = "ACCNAME"
Do While .Execute
If .Found Then
If Not oRng.InRange(oRngDup) Then Exit Do
oRng.Delete
oRng.Collapse wdCollapseEnd
bMerge = True
End If
Loop
If bMerge = True Then
oRow.Cells.Merge
oRow.Range.Font.Bold = True
oRow.Shading.BackgroundPatternColorIndex = wdGray25
End If
End With
Next oRow
Next oTbl
End Sub

mg16
07-23-2011, 02:26 AM
Thanks that worked very well