在word文档中,有时候会出现大量表格,在需要调整格式时,只能一个一个手动刷格式,这样不仅费时而且很费力。本人在写文档时,碰到这种情况,熬夜干活儿,太累。所以下定决心,抽点时间研究WORD中的宏编程。经过测试,基本上能批量改变word中的样式。下面附上本人研究VB代码,打开word中的宏。
操作方式:开发工具(如果没有,直接点击工具栏,右键,自定义工具,选择开发工具),如果有兴趣研究的话,可以采用录制宏的方式,查看其代码。
Sub Doc_tableUpdate()
'处理所有表格的Table,ActiveDocument.Tables.Count获取表格数
For i = 1 To ActiveDocument.Tables.Count
'定义为Table的第一行的颜色
ActiveDocument.Tables(i).Rows(1).Shading.ForegroundPatternColor = -570366209
'j用来控制行,m用来控制列,实现两个循环
For j = 2 To ActiveDocument.Tables(i).Rows.Count
For m = 1 To ActiveDocument.Tables(i).Columns.Count
ActiveDocument.Tables(i).Cell(j,m).Range.Bold = 0
'ActiveDocument.Tables(i).Cell(j,m).Range.Paragraphs.Format.Style
ActiveDocument.Tables(i).Cell(1,m).Range.Paragraphs.Alignment = wdAlignParagraphCenter
ActiveDocument.Tables(i).Cell(j,m).Range.Paragraphs.LeftIndent = -2
ActiveDocument.Tables(i).Cell(j,m).Range.Style.Font.Name = "宋体"
ActiveDocument.Tables(i).Cell(j,m).Range.Style.Font.Size = 12
Next m
Next j
Next i
End Sub
Sub editContent() ' ' 宏1 宏 ' 'with为重新定义正文样式 With ActiveDocument.Styles("正文").Font .NameFarEast = "黑体" .NameAscii = "+西文正文" .NameOther = "+西文正文" .Name = "+西文正文" .Size = 14 .Bold = False .Italic = False .Underline = wdUnderlineNone .UnderlineColor = wdColorAutomatic .StrikeThrough = False .DoubleStrikeThrough = False .Outline = False .Emboss = False .Shadow = False .Hidden = False .SmallCaps = False .AllCaps = False .Color = wdColorAutomatic .Engrave = False .Superscript = False .Subscript = False .Scaling = 100 .Kerning = 1 .Animation = wdAnimationNone .DisableCharacterSpaceGrid = False .EmphasisMark = wdEmphasisMarkNone .Ligatures = wdLigaturesNone .NumberSpacing = wdNumberSpacingDefault .NumberForm = wdNumberFormDefault .StylisticSet = wdStylisticSetDefault .ContextualAlternates = 0 End With With ActiveDocument.Styles("正文").ParagraphFormat .LeftIndent = CentimetersToPoints(0) .RightIndent = CentimetersToPoints(0) .SpaceBefore = 0 .SpaceBeforeAuto = False .SpaceAfter = 0 .SpaceAfterAuto = False .LineSpacingRule = wdLineSpace1pt5 .Alignment = wdAlignParagraphJustify .WidowControl = False .KeepWithNext = False .KeepTogether = False .PageBreakBefore = False .NoLineNumber = False .Hyphenation = True .FirstLineIndent = CentimetersToPoints(0.35) .OutlineLevel = wdOutlineLevelBodyText .CharacterUnitLeftIndent = 0 .CharacterUnitRightIndent = 0 .CharacterUnitFirstLineIndent = 2 .LineUnitBefore = 0 .LineUnitAfter = 0 .MirrorIndents = False .TextBoxTightWrap = wdTightNone .AutoAdjustRightIndent = True .DisableLineHeightGrid = False .FarEastLineBreakControl = True .WordWrap = True .HangingPunctuation = True .HalfWidthPunctuationOnTopOfLine = False .AddSpaceBetweenFarEastAndAlpha = True .AddSpaceBetweenFarEastAndDigit = True .BaseLineAlignment = wdBaselineAlignAuto End With ActiveDocument.Styles("正文").NoSpaceBetweenParagraphsOfSameStyle = False With ActiveDocument.Styles("正文") .AutomaticallyUpdate = False .BaseStyle = "" .NextParagraphStyle = "正文" End With End Sub Sub applyStyles() ' ' applyStyles 宏 ' ' Selection.Style = ActiveDocument.Styles("正文") End Sub Sub testApplyStyles() 'For i = 1 To ActiveDocument.Paragraphs.Count 'ActiveDocument为当前活动文档,Paragraphs为段落。该句含义为将文档所有正文使用样式正文,wdStyleNormal为正文默认,wdStyleHeading1为标题1,依此类推 ActiveDocument.Paragraphs.Style = ActiveDocument.Styles("正文") 'Next ' ActiveDocument.Styles("正文").NoSpaceBetweenParagraphsOfSameStyle = False ' With ActiveDocument.Styles("测试样式") ' .AutomaticallyUpdate = False ' .BaseStyle = "" ' .NextParagraphStyle = "测试样式" ' End With End Sub
原文链接:https://www.f2er.com/vb/257204.html