问题背景
从一份Word文档粘贴表格到另一份文档中时,因为样式设置的不同,可能会导致表格样式的错乱,包括但不限于:表格内容缩进异常、表格内中英文字体不一致、表格整体对齐效果不正确…
当需要调整的表格较多时,一个个调整费时费力,尤其Word内容过多(30w+字符)时,每次操作都要卡顿等待一段时间。处理这种重复、耗时的内容阻碍了工作的正常推进。需要高效处理文档内容以节省大量工作时间。
解决方案
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。主要能用来扩展Windows的应用程序功能。VBA却没有自己独立的工作环境,它必须依附于某一个主应用程序,专门用于Office的各应用程序中,如Word、Excel、Access等。
在Word中通过宏操作,可以实现对格式的快速、自动化设置,获得规范、一致的文档样式,并极大的提高效率。
宏功能的启用
在Word“文件-选项-自定义功能区”右侧“主选项卡”中勾选“开发者工具”,确定后即可在工具栏中看到“开发者工具”选项,
点击“宏”图标,输入宏名称,点击“创建”,进入代码编写界面。
调整当前表格格式
鼠标点击需要调整的单元格任意位置后,执行以下宏命令,即可完成表格调整。
外框1.5磅,内框0.75磅,单线
中文宋体,非中文新罗马,5号字体,首行黑体5号
表格整体页面居中
表格内容垂直居中,首行内容水平、垂直居中
单元格左右缩进0、首行缩进0、段前段后0、1.25倍行距
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 Sub 单表样式调整()With Selection.Tables(1 ) .Borders.OutsideLineStyle = wdLineStyleSingle .Borders.OutsideLineWidth = wdLineWidth150pt .Borders.InsideLineStyle = wdLineStyleSingle .Borders.InsideLineWidth = wdLineWidth075pt With .Range.Font .Size = "10.5" .NameFarEast = "宋体" .NameAscii = "Times New Roman" End With .Rows.Alignment = wdAlignRowCenter .Select Selection.Rows.AllowBreakAcrossPages = True Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter With Selection.Range.ParagraphFormat .CharacterUnitFirstLineIndent = 0 .FirstLineIndent = CentimetersToPoints(0 ) .CharacterUnitLeftIndent = 0 .LeftIndent = CentimetersToPoints(0 ) .CharacterUnitRightIndent = 0 .RightIndent = CentimetersToPoints(0 ) .SpaceBeforeAuto = False .LineUnitBefore = 0 .SpaceBefore = 0 .SpaceAfterAuto = False .LineUnitAfter = 0 .SpaceAfter = 0 .LineSpacingRule = wdLineSpaceMutiple .LineSpacing = LinesToPoints(1.25 ) .WordWrap = False End With .Cell(1 , 1 ).Select With Selection .SelectRow .ParagraphFormat.KeepWithNext = True .Borders(wdBorderBottom).LineWidth = wdLineWidth150pt .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter .Cells.VerticalAlignment = wdCellAlignVerticalCenter With .Range.Font .NameFarEast = "黑体" .NameAscii = "黑体" .Bold = False End With End With End With End Sub
调整所有表格格式
除了使用鼠标依次选中表格进行调整外,还可以遍历所有表格并依次应用对单表的调整,实现全部表格批量修改。
直接执行以下宏代码,将对文档中所有表格遍历进行调整
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 Sub 全部表格样式调整() For Each tempTable In ActiveDocument.Tables With tempTable .Borders.OutsideLineStyle = wdLineStyleSingle .Borders.OutsideLineWidth = wdLineWidth150pt .Borders.InsideLineStyle = wdLineStyleSingle .Borders.InsideLineWidth = wdLineWidth075pt With .Range.Font .Size = "10.5" .NameFarEast = "宋体" .NameAscii = "Times New Roman" End With .Rows.Alignment = wdAlignRowCenter .Select Selection.Rows.AllowBreakAcrossPages = True Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter With Selection.Range.ParagraphFormat .CharacterUnitFirstLineIndent = 0 .FirstLineIndent = CentimetersToPoints(0 ) .CharacterUnitLeftIndent = 0 .LeftIndent = CentimetersToPoints(0 ) .CharacterUnitRightIndent = 0 .RightIndent = CentimetersToPoints(0 ) .SpaceBeforeAuto = False .LineUnitBefore = 0 .SpaceBefore = 0 .SpaceAfterAuto = False .LineUnitAfter = 0 .SpaceAfter = 0 .LineSpacingRule = wdLineSpaceMutiple .LineSpacing = LinesToPoints(1.25 ) .WordWrap = False End With .Cell(1 , 1 ).Select With Selection .SelectRow .ParagraphFormat.KeepWithNext = True .Borders(wdBorderBottom).LineWidth = wdLineWidth150pt .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter .Cells.VerticalAlignment = wdCellAlignVerticalCenter With .Range.Font .NameFarEast = "黑体" .NameAscii = "黑体" .Bold = False End With End With End With Next tempTable End Sub
相关知识
在段落调整时,代码需要按照一定顺序执行 ,原因在于VBA设置格式时,对度量单位有相关要求。
对于缩进
,有字符、厘米两种常见单位,多使用字符为调整单位。但宏代码操作仅对“厘米”单位生效 。
对于间距
,有行、磅两种常见单位,且日常均有使用。但宏代码操作仅对“磅”单位生效。
因此,在设置相关样式前,要先将不可调整单位(字符、行)转换为可调整单位(厘米、磅)才能生效。以下代码作用为进行了单位转换并进行调整。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 With Selection.Range.ParagraphFormat .Alignment = wdAlignParagraphCenter .CharacterUnitFirstLineIndent = 0 .FirstLineIndent = CentimetersToPoints(0 ) .CharacterUnitLeftIndent = 0 .LeftIndent = CentimetersToPoints(0 ) .CharacterUnitRightIndent = 0 .RightIndent = CentimetersToPoints(0 ) .SpaceBeforeAuto = False .LineUnitBefore = 0 .SpaceBefore = 0 .SpaceAfterAuto = False .LineUnitAfter = 0 .SpaceAfter = 0 .LineSpacingRule = wdLineSpaceMutiple .LineSpacing = LinesToPoints(1.25 ) End With