Find.replace with loop 不能按我想要的方式工作 [英] Find.replace with loop doesn't work the way I want
问题描述
我想做什么:
- 浏览文档并查找 word1
- 用 word2 替换 word1
- 改变word2(增加它)
- 做 1-3 直到 word1 到处都被替换
到目前为止我的代码:
With ThisDocument.Range.Find ' search the Document
.Text = "RS.0569/14.xxxxx" ' for this text
.Replacement.Text = nextID ' replace it with an ID ( Start= 0 )
Do ' loop the .execute part to increment my ID and only touch one ID at a time
.Execute Replace:=wdReplaceOne ' only replace one word per loop
nextID = Val(Split(nextID, ".")(2)) + 1 ' this is just ID+1
nextID = "RS.0569/14.0000" + nextID ' put the ID together
If Not .Found Then Exit Do ' Loop should stop if everything is replaced
Loop
End With
问题:循环只运行 1 次,因为 Boolean .Found 不适用于循环 -> 我在 .execute 之前和之后做了Debug.print .Found",输出是 -> False, True", 真, 假"
Problem: The Loop only runs 1 time because the Boolean .Found doesn't work with the loop -> I did "Debug.print .Found" before and after the .execute and the output was -> "False, True, True, False"
看起来 .execute 只是切换了之前为 False 的布尔值,但是当循环重复 .execute 时,它只是将其切换为false"(因为之前是真的)
It looks like the .execute just switches the boolean which was False before but when the loop repeats the .execute it just switches it to "false" ( because it was true before)
有什么想法吗?我不知道解决方法,所以我在这里问...
Any idea? I have no idea for a workaround so I'm asking here...
推荐答案
我之前对 word 做过一些处理,.Find
对象使用起来有点棘手,尤其是因为我没有做很多 Word 自动化,但我相信这应该有效:
I have done a little bit with word before, the .Find
object is kind of tricky to work with especially because I do not do much Word automation, but I believe this should be working:
Dim fnd as Find
nextId = "RS.0569/14.00000"
Set fnd = ThisDocument.Range.Find
fnd.Text = "RS.0569/14.xxxxx"
Do
nextId = Val(Split(nextId, ".")(2)) + 1
nextId = "RS.0569/14.0000" & CStr(nextId) ' put the ID together
fnd.Replacement.Text = nextId ' replace it with an ID ( Start= 0 )
fnd.Wrap = wdFindContinue
fnd.Execute Replace:=wdReplaceOne ' only replace one word per loop
Loop Until fnd.Found = False
这是审判&错误,主要是;我认为 .Wrap
是您让它继续运行所需的东西.
It was trial & error, mostly; I think the .Wrap
is what you need to allow it to continue.
这篇关于Find.replace with loop 不能按我想要的方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!