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

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

问题描述

我已经手动完成了此操作,我想用从 1 开始一直到 Word 文档中最后一次出现 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 文档中运行 word VBA 宏,其中 VBA 宏从文件顶部开始搜索,以查找每次出现的 ZXZ,将第一次出现的 ZXZ 替换为1"然后下一次出现2",直到在word文档中找到最后一个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.

示例 word 文档可能包含:

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天全站免登陆