MS Word宏可增加Word文档中的所有数字 [英] MS Word Macro to increment all numbers in word document

查看:368
本文介绍了MS Word宏可增加Word文档中的所有数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个MS-word宏,以使字文档中括号内的所有数字递增,例如在将所有数字递增10以上之后,将原始数字[1] [2] [3] [4]递增数字将更改为[11] [12] [13] [14]

我陷入了下面的代码中,并且之前并不熟悉VBA.谁能建议,在下面的代码中添加什么以执行上面的宏?

Sub IncrementNumbers()
'
' IncrementNumbers Macro
'
'

Application.ScreenUpdating = False
Dim RngStory As Range, StrStart As String, StrEnd As String
StrStart = "["
StrEnd = "]"
Set RngStory = ActiveDocument.Range
With RngStory.Find

此处提供一些用于递增和替换数字的代码

Set RngStory = Nothing
Application.ScreenUpdating = True
End Sub

解决方案

还有另一种选择.我希望您熟悉数组的概念(任何语言).请记住,在VBA中,数组位于方括号("[1]", "[2]")中.如果没有,那将不是问题.

如果您的目标是将[1]替换为[11],将[2]替换为[12],然后将[n]替换为[n+10],则可以执行以下操作.

请考虑在此处查看.答案是一样的. 使用VBA更改文字编号

这个概念是两次处理两个数组:I)将[1]替换为@@@[1]@@@,将[2]替换为@@@[2]@@@,...,将[n]替换为@@@[n]@@@; II)将@@@[1]@@@替换为[11],将@@@[2]@@@替换为[12],...,将@@@[n]@@@替换为[n+10].

更深刻的见解:

1.1)从[1]到[n]创建searchArray.您可以使用它. http://textmechanic.com/generate-list-numbers/.前缀编号为:"[",后缀为:"]",连接为:",".

1.2)使用相同的工具创建带有前缀"@@@"和后缀"@@@"(用于唯一性)的替换数组,即@@@[1]@@@@@@[2]@@@,.. @@@[n]@@@.

1.3)将searchArray替换为replaceArray.

[附件中的适应代码] .

2.1)创建searchArray,与1.2中的相同)

2.2)使用工具 http://textmechanic.com/generate-list-numbers/从[10]到[n + 10]创建replaceArray. IE. [10][11],... [n]

2.3)将searchArray替换为replaceArray.

[附件中的适应代码] .

附件

Option Explicit
Sub replaceArrayForArray()
'
'to create array use prefix\suffix and replacing tool http://textmechanic.com/
'
'
findArray = Array("[1]", "[2]", "[3]")
replArray = Array("@@@[1]@@@", "@@@[2]@@@", "@@@[3]@@@")

For i = 0 To UBound(findArray)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = findArray(i)
        .Replacement.Text = replArray(i)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute replace:=wdReplaceAll
Next i
End Sub

PS:为什么不将[1]替换为[11]?但是我们必须首先将[1]替换为@[1]@,然后将@[1]@替换为[11]?

因为在循环中进行了10次迭代,所以我们将有两个[11]都将变成[21];然后三个[21]会变成[31]等.

PPS::如果您想复制并粘贴答案,请选择代码的两个部分:

Some code here to increment and replace numbers

Set RngStory = Nothing
Application.ScreenUpdating = True
End Sub

There is another option. I hope you are familiar with the concept of arrays (in any language). Just keep in mind that in VBA arrays are inside brackets ("[1]", "[2]"). If not, it won't be a problem.

If your goal is to replace [1] for [11], [2] for [12], ... [n] for [n+10] then you may do the following.

Please, consider to look here. The answers are alike. Changing numbering in word using VBA

The concept is to work with two arrays two times: I) replace [1] for @@@[1]@@@, [2] for @@@[2]@@@, ..., [n] for @@@[n]@@@; II) replace @@@[1]@@@ for [11], @@@[2]@@@ for [12], ..., @@@[n]@@@ for [n+10].

More profound view:

1.1) Create searchArray from [1] to [n]. You can use this. http://textmechanic.com/generate-list-numbers/. Prefix numbers with: "[", suffix with: "]", Join with: ",".

1.2) With the same tool create replaceArray with prefix "@@@" and sufix "@@@" (for uniqueness), i.e. @@@[1]@@@, @@@[2]@@@, .. @@@[n]@@@.

1.3) Replace searchArray for replaceArray.

[Adapt code from Annex].

2.1) Create searchArray, it is the same as in 1.2)

2.2) With the tool http://textmechanic.com/generate-list-numbers/ create replaceArray from [10] to [n+10]. I.e. [10], [11], ... [n]

2.3) Replace searchArray for replaceArray.

[Adapt code from Annex].

Annex

Option Explicit
Sub replaceArrayForArray()
'
'to create array use prefix\suffix and replacing tool http://textmechanic.com/
'
'
findArray = Array("[1]", "[2]", "[3]")
replArray = Array("@@@[1]@@@", "@@@[2]@@@", "@@@[3]@@@")

For i = 0 To UBound(findArray)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = findArray(i)
        .Replacement.Text = replArray(i)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute replace:=wdReplaceAll
Next i
End Sub

PS: Why not replace [1] for [11]? But we have to firstly replace [1] for @[1]@ and then secondly @[1]@ for [11]?

Because in 10 iterations through loop we will have two [11] that will both turn into [21]; then three [21] that will turn into [31] etc.

PPS: Both parts of the code if you'd like to copy and paste the answer: http://codepad.org/sZEG78ak. But still you will have to expand arrays as noted above.

这篇关于MS Word宏可增加Word文档中的所有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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