使用VB對word文件進行格式調整
在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