宏以自动为单词文档编号 [英] Macro to automatically number word documents

查看:112
本文介绍了宏以自动为单词文档编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将3000个文档合并为一个word文件,所有文件均由分节符分隔.是否有一个可以自动为每个文档按0001、0002等行编号的宏?

I've combined 3000 documents into one word file which are all separated by a section break. Is there a Macro that would automatically number each document along the lines of 0001, 0002 etc.?

推荐答案

是的,下面是代码:

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

有关在Word中查找通配符,特殊字符等的其他信息(例如^ b-分节符; ^ m-手动分节符): 查找并替换文本或其他项(support.office.com)

Further information, about finding wildcards, special characters etc in Word (like ^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位数字.
    • vbCr — new line
    • "#" & Right("0000" & "1", 4) — results in #0001. This part pushes "1" to the right of this string, but limits it to 4 digits.
    • 这篇关于宏以自动为单词文档编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆