您現在的位置是:首頁 > 藝術

word公文自動排版VBA程式碼,拿走不謝

由 桂香苑虛竹 發表于 藝術2022-08-16
簡介Font ‘字型設定

檔案排版格式怎麼設定

Sub 文件初始化() ‘公文格式初始化

Selection。WholeStory ’選擇word 所有文件

Selection。ClearFormatting ‘文件格式清除

Selection。Range。HighlightColorIndex = wdNoHighlight ’突出顯示文字取消

With Selection。Paragraphs ‘段落設定

。Alignment = wdAlignParagraphLeft ’左對齊

。LineSpacingRule = wdLineSpaceExactly ‘行距固定28。8

。LineSpacing = 28。8

。IndentFirstLineCharWidth 3 ’首行縮排2個字元

End With

With Selection。Font ‘字型設定

。Name = “仿宋_GB2312” ’字型名稱

。Size = 16 ‘三號字型

。ColorIndex = wdBlack ’黑色

End With

End Sub

Sub 標題正文設定()

With Selection。PageSetup ‘頁面設定

。TopMargin = CentimetersToPoints(3。7) ’頂端邊距

。BottomMargin = CentimetersToPoints(3。5) ‘底端邊距

。LeftMargin = CentimetersToPoints(2。8) ’左邊距

。RightMargin = CentimetersToPoints(2。6) ‘右邊距

’。PageWidth = CentimetersToPoints(18。2) ‘頁面寬度

’。PageHeight = CentimetersToPoints(25。7) ‘頁面高度

End With

’字型設定

Dim title_reg, f_reg, s_reg, th_reg, fr_reg, k, mh, strA$

Set myRange = ActiveDocument。Content

‘ 正則表示式 獲取文件內容

strA = myRange。Text

Set title_reg = CreateObject(“vbscript。regexp”)

Set f_reg = CreateObject(“vbscript。regexp”)

Set s_reg = CreateObject(“vbscript。regexp”)

Set th_reg = CreateObject(“vbscript。regexp”)

Selection。HomeKey unit:=wdStory ’游標回到文章開頭

t = 0

title_reg。Pattern = “\r\r”

‘[^\r]除了換行符之外的所有字元

title_reg。Global = True

Set Title = title_reg。Execute(strA)

With Selection。Find

。ClearFormatting

。Text = Title。Item(0)

。Execute Forward:=True

Selection。HomeKey unit:=wdStory, Extend:=wdExtend

End With

’選擇有兩個換行符的至開頭的所有段落

With Selection。Font

。Name = “方正小標宋簡體”

。Size = 22

。ColorIndex = wdBlack

End With

With Selection。Paragraphs ‘設定行距

。FirstLineIndent = CentimetersToPoints(0) ’取消首行縮排

。Alignment = wdAlignParagraphCenter ‘段落居中

。LineSpacingRule = wdLineSpaceExactly ’行距固定

。LineSpacing = Word。Application。LinesToPoints(2。3) ‘行距為2。3倍行距 一行距=12

End With

’ 以下是設定一級標題

t1 = 0 ‘初始化t1,作為一級標題是否是一是二是三是的標記,如果是,則為1,不是則為0

Selection。HomeKey unit:=wdStory

f_reg。Pattern = “(一、|二、|三、|四、|五、|六、|七、|八、|九、|十、|十一、|十二、|十三、|十四、|十五、|十六、|十七、|十八、|十九、|二十、|二十一、|二十二、|二十三、|二十四、|二十五、|二十五、|二十五、|二十六、|二十七、|二十八、|二十九、|三十、)[^\r]*\r”

f_reg。Global = True

Set f_titles = f_reg。Execute(strA)

If f_titles。Count = 0 Then ’如果一級標題是一是二是三是,則匹配

f_reg。Pattern = “(一是|二是|三是|四是|五是|六是|七是|八是|九是|十是|十一是|十二是|十三是|十四是|十五是|十六是|十七是|十八是|十九是|二十是|二十一是|二十二是|二十三是|二十四是|二十五是|二十六是|二十七是|二十八是|二十九是|三十是)([^。])*。”

Set f_titles = f_reg。Execute(strA)

t1 = 1

End If

For Each f_title In f_titles

With Selection。Find

。ClearFormatting

。Text = f_title。Value

Debug。Print “一級標題遍歷專案:”; f_title。Value

。Execute Forward:=True

End With

With Selection。Font

。Name = “黑體”

。Size = “16”

。ColorIndex = wdBlack

End With

Selection。HomeKey unit:=wdStory

Next

‘ 以下是設定二級標題

If t1 = 0 Then ’p判斷一級標題是否是一是二是三是的標記,如果是0,則不是一是二是三是,則執行,不是則不執行

t2 = 0

Selection。HomeKey unit:=wdStory

s_reg。Global = True

s_reg。Pattern = “((一)|(二)|(三)|(四)|(五)|(六)|(七)|(八)|(九)|(十)|(十一)|(十二)|(十三)|(十四)|(十五)|(十六)|(十七)|(十八)|(十九)|(二十)|(二十一)|(二十二)|(二十三)|(二十四)|(二十五)|(二十六)|(二十七)|(二十八)|(二十九)|(三十))([^。\r:])*[。|\r:]” ‘排除句號和段落符號查詢所有,找到句號或段落符號後停止

Set s_titles = s_reg。Execute(strA)

If s_titles。Count = 0 Then ’如果二級標題是一是二是三是,則匹配

s_reg。Pattern = “(一是|二是|三是|四是|五是|六是|七是|八是|九是|十是|十一是|十二是|十三是|十四是|十五是|十六是|十七是|十八是|十九是|二十是|二十一是|二十二是|二十三是|二十四是|二十五是|二十六是|二十七是|二十八是|二十九是|三十是)([^。])*。”

Set s_titles = s_reg。Execute(strA)

t2 = 1

End If

For Each s_title In s_titles

With Selection。Find

。ClearFormatting

。Text = s_title。Value

Debug。Print “二級標題遍歷專案:”; s_title。Value

。Execute Forward:=True

End With

With Selection。Font

。Name = “楷體”

。Size = “16”

。ColorIndex = wdBlack

。Bold = True

End With

Selection。HomeKey unit:=wdStory

Next

End If

‘ 以下是設定三級標題

If t2 = 0 Then

Selection。HomeKey unit:=wdStory

th_reg。Global = True

th_reg。Pattern = “\r\d{1,2}\。([^。])*。”

Set th_titles = th_reg。Execute(strA)

If th_titles。Count = 0 Then ’如果三級標題是一是二是三是,則匹配

th_reg。Pattern = “(一是|二是|三是|四是|五是|六是|七是|八是|九是|十是|十一是|十二是|十三是|十四是|十五是|十六是|十七是|十八是|十九是|二十是|二十一是|二十二是|二十三是|二十四是|二十五是|二十六是|二十七是|二十八是|二十九是|三十是)([^。])*。”

Set th_titles = th_reg。Execute(strA)

End If

For Each th_title In th_titles

With Selection。Find

。ClearFormatting

。Text = th_title。Value

Debug。Print “三級標題遍歷專案:”; th_title。Value

。Execute Forward:=True

End With

With Selection。Font

。Bold = True

。ColorIndex = wdBlack

End With

Selection。HomeKey unit:=wdStory

Next

End If

End Sub

Sub 頁碼設定()

ActiveDocument。Sections(1)。Footers(wdHeaderFooterPrimary)。PageNumbers。Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True

With ActiveDocument。Sections(1)。Footers(wdHeaderFooterPrimary) ‘進入頁尾編輯狀態

。Range。Font。Size = 15

。Range。Font。Name = “仿宋”

。Range。Collapse Direction:=wdCollapseEnd

End With

End Sub

Sub 刪除頁首橫線()

With ActiveDocument。Sections(1)。Headers(wdHeaderFooterPrimary)。Range ’進入頁尾編輯狀態

。Delete ‘刪除頁首中的內容

。ParagraphFormat。Borders(wdBorderBottom)。LineStyle = wdLineStyleNone ’段落下邊框線

End With

End Sub

Sub 公文格式排版()

Call 文件初始化

Call 標題正文設定

Call 頁碼設定

Call 刪除頁首橫線

End Sub

推薦文章