Word VBA-表格操作汇总

===========================

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

===========================

===========================

===========================

===========================

===========================

===========================

===========================

===========================

===========================

===========================

===========================

===========================

===========================

===========================

 

 
相关推荐

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

xinlan3618

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值