===========================
Sub 遍历表格设定样式()
For Each aTable In ActiveDocument.Tables
With aTable
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
Next aTable
End Sub
===========================
'遍历表格的操作
Dim aTable As Table
'第一种实现方式,根据索引遍历表格,特定单元格赋值内容
For i = 1 To ActiveDocument.Tables.Count
Set aTable = ActiveDocument.Tables.Item(i)
With aTable
.Cell(2, 1).Range.Text = "123"
End With
Next
'第二种实现方式
For Each aTable In ActiveDocument.Tables
With aTable
.Cell(2, 1).Range.Text = "123"
End With
Next
===========================
'判断表格列标题,针对不同标题列设置不同样式
With aTable
For i = 1 To .Columns.Count
If InStr(.Cell(1, i).Range.Text, "序号") Or _
InStr(.Cell(1, i).Range.Text, "数量") Or _
InStr(.Cell(1, i).Range.Text, "单位") Then
(Code代码段)
End If
Next
End With
===========================
'表格样式设置代码汇总
With aTable
'将预定义外观应用于表格。
' .AutoFormat Format:=wdTableFormatColorful2
' .AutoFormat applyborders:=True
' .AutoFormat ApplyShading:=True
' .AutoFormat ApplyFont:=True
' .AutoFormat ApplyColor:=True
' .AutoFormat ApplyHeadingRows:=True
' .AutoFormat ApplyLastRow:=True
' .AutoFormat ApplyFirstColumn:=True
' .AutoFormat ApplyLastColumn:=True
' .AutoFormat AutoFit:=True
'应用用户定义的样式,但保留用户直接应用的所有格式。
' .ApplyStyleDirectFormatting StyleName:="样式1"
'调整行列的样式,必须使用Select的方法,先选中再设置样式
'调整首行的对齐方式
' .Rows(1).Select
' With Selection
' .ParagraphFormat.Alignment = wdAlignParagraphCenter
' End With
'调整各列的对齐方式
' .Columns(3).Select
' With Selection
' .ParagraphFormat.Alignment = wdAlignParagraphCenter
' End With
' .Columns(4).Select
' With Selection
' .ParagraphFormat.Alignment = wdAlignParagraphCenter
' End With
'操作单元格内容赋值
' .Cell(1, 1).Range.Text = "hello"
End With
===========================
===========================
===========================
===========================
===========================
===========================
===========================
===========================
===========================
===========================
===========================
===========================
===========================
===========================