[英]Macro to automatically number word documents
我將 3000 個文檔合並到一個 word 文件中,這些文件都由分節符分隔。 是否有一個宏可以沿着 0001、0002 等行自動為每個文檔編號?
是的,這里的代碼是:
有關在 Word 中查找通配符、特殊字符等的更多信息(如 ^b - 分節符;^m - 手動中斷): 查找和替換文本或其他項目 (support.office.com)
這是代碼:
Option Explicit
Sub changeSectionsForPageBreaksAndNumbers()
Dim i, countSections, sectionNumber
countSections = ActiveDocument.Sections.Count
'Loop that changes section breaks for page break + # + number of the section (from 2 to last section)
For i = 1 To countSections Step 1
sectionNumber = i + 1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^b"
.Replacement.Text = "^m" & "#" & Right("0000" & sectionNumber, 4) & vbCr
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne
Selection.MoveRight Unit:=wdCharacter, Count:=1
Next
'first number in the beginning of the document
Selection.HomeKey Unit:=wdStory
Selection.InsertBefore "#0001" & vbCr
MsgBox ("Total sections: " & countSections)
End Sub
vbCr
— 新行"#" & Right("0000" & "1", 4)
— 結果是#0001。 這部分將“1”推到此字符串的右側,但將其限制為 4 位。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.