我将3000个文档合并到一个word文件中,这些文件都被分节符分开。是否有一个宏可以自动为0001,8000等行的每个文档编号?
答案 0 :(得分:1)
是的,这是代码:
有关在Word中查找通配符,特殊字符等的更多信息(例如^ b - section break; ^ m - manual break): 的 Find and replace text or other items (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位数。