PDA

View Full Version : Search and replace in table



Tom0ne
12-30-2020, 04:03 PM
Hello all

I have multiple word documents with a lot of tables inside each.
All the tables have the same format.

The first line of the table is always grey background.

I need create a vba macro who can search and replace the background color of the first ligne table, change the color of the tables border and change font color of text when backgound is grey.

I have tried something with word styles but it is not optimal,
Please look test here (macro named TABLEAU)
https://we.tl/b-BXxwZXOZGS

Thanks a lot for help

Tom

gmaxey
12-31-2020, 01:21 PM
Sub YourMatchMacro()
ScratchMacro ActiveDocument
End Sub

Sub ScratchMacro(oDoc)
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Set oDoc = ActiveDocument
For Each oTbl In ActiveDocument.Tables
With oTbl
If .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25 Then
.Rows(1).Range.Shading.BackgroundPatternColorIndex = wdWhite
.Rows(1).Range.Font.ColorIndexBi = wdBrightGreen
.Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25
.Borders.OutsideColor = wdColorBrightGreen
.Borders.InsideColor = wdColorGold
End If
End With
Next
lbl_Exit:
Exit Sub
End Sub

Tom0ne
12-31-2020, 03:55 PM
Sub YourMatchMacro()
ScratchMacro ActiveDocument
End Sub

Sub ScratchMacro(oDoc)
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Set oDoc = ActiveDocument
For Each oTbl In ActiveDocument.Tables
With oTbl
If .Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25 Then
.Rows(1).Range.Shading.BackgroundPatternColorIndex = wdWhite
.Rows(1).Range.Font.ColorIndexBi = wdBrightGreen
.Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25
.Borders.OutsideColor = wdColorBrightGreen
.Borders.InsideColor = wdColorGold
End If
End With
Next
lbl_Exit:
Exit Sub
End Sub


Thanks a lot Greg,
it's really very very friendly to have written this code to help me.
it's really very pleasant because tonight I'm very bored, locked up at home because of the Coronavirus.

I have tried, modify a little bit your code for testing,
You can see it here :

https://we.tl/b-x17c3pgecS

Could you help me more for adjust it?
1 - The following line do not apply any change
Borders.InsideColor = wdColorGold
(I want apply a color at all borders of the tables if
.Rows(1).Range.Shading.BackgroundPatternColorIndex = wdGray25 Then)
I need adjust size of all border too

2 - If you look at the first table on the document you'll can see that the first rows is divided into three cells.
Then the code is not applied at this table.
The best will be to search grey background cell and apply code.

I hope i explain it clearly :)

Thanks a lot and very good new year, i hope better than 2020.

Thomas

Tom0ne
01-08-2021, 04:33 PM
Help please :)
Can i ask a little up?

Thanks all and happy new year to all

macropod
01-09-2021, 02:37 PM
I have tried, modify a little bit your code for testing,
You can see it here :

https://we.tl/b-x17c3pgecS

Could you help me more for adjust it?
Kindly attach any documents etc you want people to view to your post here instead of loading on third-party sites where people have to sign up to access your files.

You can attach files via 'Go Advanced' at the bottom of the posting/reply screen, then selecting the paper-clip symbol.