Word VBA Range.Find 对象工作不正确

发布于 2024-09-28 05:37:12 字数 262 浏览 1 评论 0原文

我发现一个有趣的事情:

在word 2010中,选择一些文本,然后运行以下VBA代码:

public Sub Test()
    With Selection.Range.Find
        MsgBox .Execute(Selection.Range.text) 
        MsgBox .Found
    End With
End Sub

两个消息框都说“False”,但都应该是“True”。为什么?

I found an interesting thing:

In word 2010, select some text, and run the following VBA code:

public Sub Test()
    With Selection.Range.Find
        MsgBox .Execute(Selection.Range.text) 
        MsgBox .Found
    End With
End Sub

Both the two message box say "False", but both should be "True". Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2024-10-05 05:37:12

非常有趣!

Word 2007 也表现出相同的行为。

奇怪的是,这不仅在您从 VBA 搜索时出现:如果您在文档中选择某些文本并按“查找”,查找对话框将默认显示所选文本作为目标。
按“查找下一个>” Word 显示错误消息“Word 已到达文档末尾...等”。

如果您修改文本以搜索并删除最后一个字符,则不会发生这种情况。 VBA 与此一致:以下代码有效!

Sub tt()
    With Selection.Range.Find    
       MsgBox .Execute(Mid(Selection.Range.Text,1,Len(Selection.Range.Text)-1))  
       MsgBox .Found  
    End With  
End Sub

没有解决...但证明与GUI是一致的。

哈!

Very interesting!

Word 2007 exhibits the same behaviour.

The curious thing is that this not only when you are searching from VBA: If you select some text in the doc and press "find", the find dialog displays as default the selected text as objective.
By pressing "Find Next >" Word displays an error message "Word has reached the end of the document ... etc".

This doesn't happen if you modify the text to search deleting the last character. VBA is consistent with that: the folowing code works!

Sub tt()
    With Selection.Range.Find    
       MsgBox .Execute(Mid(Selection.Range.Text,1,Len(Selection.Range.Text)-1))  
       MsgBox .Found  
    End With  
End Sub

Not solved ... but proved to be consistent with the GUI.

HTH!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文