Word VBA-表格操作汇总

该博客介绍了如何使用VBA在Word中遍历表格并统一设置样式,包括边框、填充和文字颜色等。同时,展示了如何为特定列标题设置样式,并提供了单元格内容批量赋值的方法。内容涵盖了表格格式自动化和个性化调整,适用于大量文档格式整理工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值