This problem has already been encountered for the second time. I feel that the office is very unreliable and the documents I have worked hard to write are all well done before. Then I opened them one day and found that all the tables in it have no borders, strange... the most terrible thing is that you need to submit this document immediately. At this time, macros will be used.
First, let's look at a macro that traverses all tables:
Sub hong2 ()
Dim TB as table
With activedocument
For each TB in. Tables
Next
End
End sub
It's easy. We won't go around in circles. Let's get the result directly:
Sub hong2 ()
Dim TB as table
With activedocument
For each TB in. Tables
With TB
With. Borders (wdborderleft)
. Linestyle = wdlinestylesingle
. Linewidth = wdlinewidth050pt
. Color = wdcolorautomatic
End
With. Borders (wdborderright)
. Linestyle = wdlinestylesingle
. Linewidth = wdlinewidth050pt
. Color = wdcolorautomatic
End
With. Borders (wdbordertop)
. Linestyle = wdlinestylesingle
. Linewidth = wdlinewidth050pt
. Color = wdcolorautomatic
End
With. Borders (wdborderbottom)
. Linestyle = wdlinestylesingle
. Linewidth = wdlinewidth050pt
. Color = wdcolorautomatic
End
With. Borders (wdborderhorizontal)
. Linestyle = wdlinestylesingle
. Linewidth = wdlinewidth050pt
. Color = wdcolorautomatic
End
With. Borders (wdbordervertical)
. Linestyle = wdlinestylesingle
. Linewidth = wdlinewidth050pt
. Color = wdcolorautomatic
End
. Borders (wdborderdiagonaldown). linestyle = wdlinestylenone
. Borders (wdborderdiagonalup). linestyle = wdlinestylenone
. Borders. Shadow = false
End
Next
End
With options
. Defaultborderlinestyle = wdlinestylesingle
. Defaultborderlinewidth = wdlinewidth050pt
. Defaultbordercolor = wdcolorautomatic
End
End sub
Haha, finished ....
Paste this code into the macro editor and run it. OK, the problem is solved...
Based on this, we can also derive some other macros. The following is what I wrote for the sake of time:
Automatically select all tables:
Sub selectalltables ()
Dim mytable as table
Application. screenupdating = false
For each mytable in activedocument. Tables
Mytable. range. Editors. Add wdeditoreveryone
Next
Activedocument. selectalleditableranges (wdeditoreveryone)
Activedocument. deletealleditableranges (wdeditoreveryone)
Application. screenupdating = true
End sub
Traverse all tables and select the headers in sequence:
Sub selecttableheader ()
Dim TB as table
With activedocument
For each TB in. Tables
. Range (Tb. Cell (1, 1). range. Start, TB. Cell (1, TB. Columns. Count). range. End). Select
Next
End
End sub
Traverse all headers and set the header background to light gray:
Sub hong3 ()
Dim TB as table
With activedocument
For each TB in. Tables
. Range (Tb. Cell (1, 1). range. Start, TB. Cell (1, TB. Columns. Count). range. End). Select
Selection. shading. backgroundpatterncolor = wdcolorgray20
Next
End
End sub
The following code implements the same function:
Sub hong4 ()
Dim TB as table
With activedocument
For each TB in. Tables
. Range (Tb. Cell (1, 1). range. Start, TB. Cell (1, TB. Columns. Count). range. End). shading. backgroundpatterncolor = wdcolorgray20
Next
End
End sub
The following code sets the font and color of the header:
Sub hong5 ()
Dim TB as table
With activedocument
For each TB in. Tables
. Range (Tb. Cell (1, 1). range. Start, TB. Cell (1, TB. Columns. Count). range. End). Font. Color = wdcolorred
. Range (Tb. Cell (1, 1). range. Start, TB. Cell (1, TB. Columns. Count). range. End). Font. size = 9
Next
End
End sub