VBA Microsoft Word-序号 [英] VBA Microsoft Word - sequential numbers

查看:101
本文介绍了VBA Microsoft Word-序号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手动完成了此操作,在这里我想用从1一直到单词文档中最后出现的ZXZ的序列号替换ZXZ的每个实例.

I have done this manually where I want to replace every instance of ZXZ with a sequential number starting from 1 all the way to the last occurrence of ZXZ in the word document.

基本上,我要实现的是在特定的word文档中运行一个单词VBA宏,其中VBA宏从文件顶部开始搜索以查找每次出现的ZXZ,并用替换第一次出现的ZXZ依次为"1"和下一个出现的"2",直到在单词文档中找到最后一个ZXZ.

Basically, what I am trying to achieve is to run a word VBA macro within a specific word document, where the VBA macro starts searching from the top of the file looking for each occurrence of ZXZ, replaces the first occurrence of ZXZ with "1" and then next occurrence with "2" sequentially until the last ZXZ found in a word document.

示例文字文档可能包含:

Example word document could contain:

元素ZXZ 元素ZXZ ... 元素ZXZ

element ZXZ element ZXZ ... element ZXZ

运行vba word宏后,我想结束:

after running the vba word macro, I would like to end up with:

元素1 元素2 ... 元素25

element 1 element 2 ... element 25

我已经使用这段代码完成了此操作,但是我想执行"while"循环或查找每次出现的ZXZ并将其替换为从"1"开始的序号的东西

I've done this with this code but I would like to do "while" loop or something that finds each occurrence of ZXZ and replaces it with sequential numbers starting from "1"

    Sub my_prov_MDList()
'
' my_prov_MDList Macro
'
'
    Selection.MoveUp Unit:=wdScreen, Count:=7
    Selection.HomeKey Unit:=wdLine
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "ZXZ"
        .Replacement.Text = "1"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    With Selection
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseStart
        Else
            .Collapse Direction:=wdCollapseEnd
        End If
        .Find.Execute Replace:=wdReplaceOne
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseEnd
        Else
            .Collapse Direction:=wdCollapseStart
        End If
        .Find.Execute
    End With
    With Selection.Find
        .Text = "ZXZ"
        .Replacement.Text = "2"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    With Selection
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseStart
        Else
            .Collapse Direction:=wdCollapseEnd
        End If
        .Find.Execute Replace:=wdReplaceOne
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseEnd
        Else
            .Collapse Direction:=wdCollapseStart
        End If
        .Find.Execute
    End With

''''''我基本上一直重复上述代码,直到达到25,这通常是每个文档中存在ZXZ实例的数量.

''''I basically keep repeating the above code until I get to 25, which is typically how many instances of ZXZ exist in each document.

推荐答案

效率更高:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "ZXZ"
    .Replacement.Text = ""
    .Format = False
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    i = i + 1
    .Text = i
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub

这篇关于VBA Microsoft Word-序号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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