单词vba循环如果find.found [英] word vba loop do if find.found

查看:119
本文介绍了单词vba循环如果find.found的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Word VBA遍历文档,如果发现,我想做的就是.Text = A& vbTab ,然后我将其替换为 A: ,然后转到行尾,按一次DEL键,然后键入< br> 。但是我无法管理它,当我运行这段代码时,单词总是崩溃。.

I'm trying to use Word VBA to loop through a document and what I'm trying to do is, if I find .Text = "A" & vbTab, then I want to replace it with "A: ", then go to the end of the line, press the DEL key once and type <br>. But I couldn't manage it, somehow word keeps crashing when I run this code..

Sub marx()
Dim r As Range
Set r = ActiveDocument.Range

r.Find.ClearFormatting
With r.Find
    .Text = "A" & vbTab
    .Replacement.Text = "A: "
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
End With

Do While r.Find.Found

Selection.EndKey Unit:=wdLine
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="<br>"
r.Find.Execute
Loop

End Sub


推荐答案

它可能由于代码进入无限循环而崩溃-因为Find一直在执行,并且由于您不替换,所以它一直在寻找

It's probably crashing because the code goes into an infinite loop - because Find keeps executing and, since you don't replace the found term it keeps finding the same search term over and over again.

即使没有发生并且您确实做了替换,结果也不会是您期望的,因为选择

Even if that weren't happening and you did do a Replace, the result wouldn't be what you expect because the Selection would still be what it was when you started the macro.

首先,您需要执行一次替换,其次,您需要选择找到的范围(并且转到您需要选择的行的末尾)。最后,循环只需要一个Find.Execute。

First, you need to execute a Replace and secondly, you need to Select the found Range (and in order to go to the end of a line you do need Selection). Lastly, the loop needs only one Find.Execute.

这是成功解决问题的一种方法。请注意,我使用布尔值来选择搜索是否成功,并针对Do循环以及是否应该执行Selection操作进行测试。

Here's one way to successfully approach your problem. Note that I use a boolean to pick up whether the search was successful and test that, both for the Do loop as well as for whether the Selection actions should be performed.

还要注意 Find.Execute 中的 Replace 参数-只需设置 Replacement.Text 还不够。

Note also the Replace argument in Find.Execute - just setting Replacement.Text isn't enough.

然后看看我是如何使用 r.Select 的。 。

And look at how I use r.Select before the Selection actions.

Sub marx()
    Dim r As Range
    Dim bFound As Boolean

    bFound = True
    Set r = ActiveDocument.content

    r.Find.ClearFormatting
    Do While bFound
        With r.Find
            .Text = "A" & vbTab
            .Replacement.Text = "A: "
            .Forward = True
            .wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            bFound = .Execute(Replace:=wdReplaceOne)
        End With

        If bFound Then
            r.Select
            Selection.EndKey Unit:=wdLine
            Selection.Delete Unit:=wdCharacter, Count:=1
            Selection.TypeText Text:="<br>"
        End If
    Loop    
End Sub

这篇关于单词vba循环如果find.found的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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