VBA Microsoft Word-序号

时间:2018-08-23 16:04:30

标签: vba ms-word

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

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

示例文字文档可能包含:

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

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

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

我已经使用这段代码完成了此操作,但是我想执行“ while”循环或查找每次出现的ZXZ并将其替换为从“ 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实例的数量。

2 个答案:

答案 0 :(得分:3)

效率更高:

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

答案 1 :(得分:1)

可能不是最有效的方法...但是这里是:

Option Explicit

Function countOccurrences(someString As String) As Integer

    Dim iCount As Long

    If someString > "" Then
        Application.ScreenUpdating = False
        With Selection
            .HomeKey Unit:=wdStory
            With .Find
                .ClearFormatting
                .Text = someString
                Do While .Execute
                    iCount = iCount + 1
                    Selection.MoveRight
                Loop
            End With
        End With
        Application.ScreenUpdating = True
    End If

    countOccurrences = iCount

End Function

Sub my_prov_MDList()

    Dim counter As Long
    For counter = 1 To countOccurrences("ZXZ")

        Selection.MoveUp Unit:=wdScreen, count:=7
        Selection.HomeKey Unit:=wdLine
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting

        With Selection.Find
            .Text = "ZXZ"
            .Replacement.Text = CStr(counter)
            .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

    Next

End Sub

只需添加一个功能即可计算出现“ ZXZ”的次数,因此可以将其用作循环限制