【问题标题】:Macro to automatically number word documents自动编号word文档的宏
【发布时间】:2020-07-06 23:54:47
【问题描述】:

我已将 3000 个文档合并到一个 word 文件中,这些文件都由分节符分隔。是否有一个宏可以自动按照 0001、0002 等为每个文档编号?

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    是的,代码如下:

    • 找到分节符 (^b)
    • 将其更改为手动分页符 (^m) 及其数量。

    有关在 Word 中查找通配符、特殊字符等的更多信息(如 ^b - 分节符;^m - 手动分节符): 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 位。

    【讨论】:

    • 干杯,伙计,最有帮助的。一切顺利,克里斯蒂安
    • @Christian,如果这解决了您的问题,您可以将答案勾选为“已接受”。
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2020-12-08
    • 1970-01-01
    • 2014-12-04
    • 2011-01-23
    • 2021-12-15
    • 1970-01-01
    • 2014-12-21
    相关资源
    最近更新 更多