文档库 最新最全的文档下载
当前位置:文档库 › Word VBA 公文格式调整

Word VBA 公文格式调整


Sub 网页内容修改格式()
'
' 网页内容修改格式 宏
'
' 设置格式为A4通用格式
With ActiveDocument.Styles(wdStyleNormal).Font
If .NameFarEast = .NameAscii Then
.NameAscii = ""
End If
.NameFarEast = ""
End With
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait '横向
.TopMargin = CentimetersToPoints(2.54) '上边距
.BottomMargin = CentimetersToPoints(2.54)
.LeftMargin = CentimetersToPoints(3.17)
.RightMargin = CentimetersToPoints(3.17)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.5) '页眉
.FooterDistance = CentimetersToPoints(1.75)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
.LayoutMode = wdLayoutModeLineGrid
End With

'
'去掉硬回车
'Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
'Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchByte = True
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll


'去掉多余空行

' With Selection.Find
' .Text = "[^13]{2,}"
' .Replacement.Text = "^p"
' .Forward = True
' .Wrap = wdFindContinue
' .Format = False
' .MatchByte = True
' .MatchWildcards = True
' End With
' Selection.Find.Execute Replace:=wdReplaceAll
ActiveWindow.ActivePane.VerticalPercentScrolled = 0

'英文空格
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


'单引号变双引号
Selection.Find.Execute Replace:=wdReplaceAll
With Sele

ction.Find
.Text = """(*)"""
.Replacement.Text = ChrW(8220) & "\1" & ChrW(8221)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll


'删除前面的空行-只删除回车
Selection.HomeKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
While Trim(Selection.Text) = Chr(13)
Selection.Delete
Selection.HomeKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Wend
Selection.Find.Replacement.ClearFormatting


'删除后面的空行
Selection.EndKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
While Trim(Selection.Text) = Chr(13)
Selection.Delete
Selection.EndKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Wend
Selection.Find.Replacement.ClearFormatting

'格式设置
'选中整篇文档,设置中文为仿宋,英文为tnr
Selection.WholeStory
Selection.Font.Size = 15
https://www.wendangku.net/doc/4c13944678.html, = "仿宋_GB2312"
https://www.wendangku.net/doc/4c13944678.html, = "Times New Roman"
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

Selection.ParagraphFormat.LeftIndent = CentimetersToPoints(0) '左缩进为0
Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 2 '首行缩进
'段落修改为行间距28
On Error Resume Next
With Selection.ParagraphFormat
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 28
End With



'选中第一行,设置字体,尝试大小两种,如超过则换小的字体
'先选中第一行,检查是否有回车,如无,则选中两行,设置格式为二号字体,再重复检查是否是两行,两行则重新设置字体为小二或三号
Selection.HomeKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend

Set myRange = Selection.Range
myRange.Find.Execute FindText:="^p", Forward:=True
If myRange.Find.Found = False Then
Selection.HomeKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.MoveDown unit:=wdLine, Count:=1, Extend:=wdExtend
Else
Selection.HomeKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
End If
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
https://www.wendangku.net/doc/4c13944678.html, = "宋体"
Selection.Font.Size = 22
Selection.Font.Bold = True
Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 0 '首行缩进
Selection.ParagraphFormat.FirstLineIndent = Cen

timetersToPoints(0)



Selection.HomeKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend

Set myRange = Selection.Range
myRange.Find.Execute FindText:="^p", Forward:=True
If myRange.Find.Found = False Then
Selection.HomeKey unit:=wdStory
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.MoveDown unit:=wdLine, Count:=1, Extend:=wdExtend
https://www.wendangku.net/doc/4c13944678.html, = "宋体"
Selection.Font.Size = 18
End If



'检测最后一页的内容,少于4行就修改行间距
Selection.EndKey unit:=wdStory
h3 = 28
While (https://www.wendangku.net/doc/4c13944678.html,rmation(wdFirstCharacterLineNumber) < 5) And (https://www.wendangku.net/doc/4c13944678.html,rmation(wdActiveEndPageNumber) > 1)
'减少行间距
If Application.ScreenUpdating <> ture Then
Application.ScreenUpdating = ture
End If
If https://www.wendangku.net/doc/4c13944678.html,youtMode <> wdLayoutModeDefault Then
https://www.wendangku.net/doc/4c13944678.html,youtMode = wdLayoutModeDefault
End If
Selection.WholeStory
With Selection.Paragraphs
.LineSpacingRule = wdLineSpaceExactly
h3 = h3 - 1
.LineSpacing = h3
End With
Selection.EndKey unit:=wdStory
' If h3 < 23 Then

' End If
Wend

'检测最后行是否要右对齐
Selection.EndKey unit:=wdStory
Selection.EndKey unit:=wdLine
Selection.HomeKey unit:=wdLine, Extend:=wdExtend
Selection.MoveUp unit:=wdLine, Count:=1, Extend:=wdExtend

Set myRange = Selection.Range
myRange.Find.Execute FindText:="二〇一", Forward:=True
If myRange.Find.Found = True Then
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

' Selection.TypeText Text:=Trim(Selection.Text)
End If
myRange.Find.Execute FindText:="201", Forward:=True
If myRange.Find.Found = True Then
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
End If
myRange.Find.Execute FindText:="年", Forward:=True
If myRange.Find.Found = True Then
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
End If

Selection.EndKey unit:=wdStory
Selection.EndKey unit:=wdLine
Selection.HomeKey unit:=wdLine, Extend:=wdExtend
If Selection.ParagraphFormat.Alignment = wdAlignParagraphRight Then
Selection.TypeText Text:=Trim(Selection.Text)
Selection.MoveUp unit:=wdLine, Count:=1
Selection.EndKey unit:=wdLine
Selection.HomeKey unit:=wdLine, Extend:=wdExtend
Selection.TypeText Text:=Trim(Selection.Text)

End If



End Sub


相关文档
相关文档 最新文档