MS Word 宏可增加 Word 文档中的所有数字 [英] MS Word Macro to increment all numbers in word document
问题描述
我正在尝试制作一个 MS-word 宏来增加括号内 word 文档中的所有数字,例如原始数字 [1] [2] [3] [4] ,在将所有数字增加 10 之后,上面数字将更改为 [11] [12] [13] [14]
我被下面的代码困住了,以前不熟悉 VBA.任何人都可以建议,在下面的代码中添加什么来执行上面的宏?
Sub IncrementNumbers()'' IncrementNumbers 宏''Application.ScreenUpdating = FalseDim RngStory 作为范围,StrStart 作为字符串,StrEnd 作为字符串StrStart = "["StrEnd = "]"设置 RngStory = ActiveDocument.Range使用 RngStory.Find
这里有一些代码来增加和替换数字
设置 RngStory = 无Application.ScreenUpdating = True结束子
还有另一种选择.我希望您熟悉数组的概念(在任何语言中).请记住,在 VBA 中,数组位于方括号 ("[1]", "[2]")
内.如果没有,那就没问题了.
如果您的目标是将 [1]
替换为 [11]
,将 [2]
替换为 [12]
, ... [n]
for [n+10]
那么你可以做以下操作.
请考虑看这里.答案都是一样的.使用 VBA 更改 word 中的编号>
这个概念是使用两个数组两次:I) 将 [1]
替换为 @@@[1]@@@
, [2]
for @@@[2]@@@
, ..., [n]
for @@@[n]@@@代码>;II) 将
@@@[1]@@@
替换为 [11]
,将 @@@[2]@@@
替换为 >[12]
, ..., @@@[n]@@@
for [n+10]
.
更深刻的观点:
1.1) 从 [1] 到 [n] 创建 searchArray.你可以用这个.http://textmechanic.com/generate-list-numbers/.数字前缀:[
",后缀:]
",加入:,
".
1.2) 使用相同的工具创建带有前缀@@@
"和后缀@@@
"的replaceArray(为了唯一性),即@@@[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.
[改编附件中的代码].
附件
选项显式子 replaceArrayForArray()''创建数组使用前缀后缀和替换工具 http://textmechanic.com/''findArray = Array("[1]", "[2]", "[3]")replArray = Array("@@@[1]@@@", "@@@[2]@@@", "@@@[3]@@@")对于 i = 0 到 UBound(findArray)选择.查找.清除格式Selection.Find.Replacement.ClearFormatting使用选择.查找.Text = findArray(i).Replacement.Text = replArray(i).Forward = 真.Wrap = wdFindContinue.Format = 假.MatchCase = 真.MatchWholeWord = 假.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = False结束于Selection.Find.Execute replace:=wdReplaceAll接下来我结束子
PS: 为什么不将 [1]
替换为 [11]
?但是我们必须首先将 [1]
替换为 @[1]@
然后将 @[1]@
替换为 [11]
?
因为在循环的 10 次迭代中,我们将有两个 [11]
,它们都将变成 [21]
;然后三个 [21]
将变成 [31]
等等.
PPS:如果您想复制和粘贴答案,请使用两部分代码:http://codepad.org/sZEG78ak.但是你仍然需要像上面提到的那样扩展数组.
I am trying to make a MS-word Macro to increment all numbers in the word document which are within brackets, eg original numbers [1] [2] [3] [4] , after incrementing all numbers by 10, above numbers will be changed to [11] [12] [13] [14]
I'm stuck in the code below and not familiar with VBA before. Can anyone suggest, what to add in code below to perform above macro??
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
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 prefixsuffix 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屋!