“运行时错误 5692”是什么意思?意思是?

发布于 2024-09-26 16:59:25 字数 155 浏览 6 评论 0原文

在某些情况下,range.Find.Execute 会抛出错误“运行时错误 5692”,包括有时当范围为空时,有时当使用正则表达式搜索完成时格式错误的正则表达式。

这个错误是什么意思?错误是否记录在任何地方?以我的无知来说,这是不可预测的。

The error "Runtime error 5692" gets thrown by range.Find.Execute under some circumstances, including sometimes when the range is empty, and sometimes when a regex search is done with a malformed regex.

What does this error mean? Is the error documented anywhere? From my position of ignorance, it is unpredictable.

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

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

发布评论

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

评论(2

青柠芒果 2024-10-03 16:59:25

根据我对 Word 2007(在 Crossover FWIW 下)的测试,我认为当“查找”文本 (Find.Text) 包含 Find.MatchWildcards 标志的无效字符时,就会发生这种情况。该标志是粘性的,因此,如果用户在上次手动查找或查找/替换时碰巧使用它,则 VBA 脚本将受到该粘性设置的污染,从而在您使用“^p”等字符时导致运行时错误 5692 和Find.Text 字符串中的“^t”。在运行 Find.Execute 之前显式设置“Find.MatchWildcards = False”可以防止出现错误。您可能会认为我们可以有一些文档或更好的错误消息。

总是失败的测试用例:

Sub ReplaceInDocument
  Dim wdReplaceAll
  'set the value for the replace "constant"
  wdReplaceAll = 2
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p"
    .Replacement.Text = "replacement text "
    .Forward = True
    .Wrap = 1
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  'the Replace argument is the 11'th argument
    .Execute , , , , , , , , , , wdReplaceAll
  End With
End Sub

有效的测试用例:

Sub ReplaceInDocument
  Dim wdReplaceAll
  'set the value for the replace "constant"
  wdReplaceAll = 2
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p"
    .Replacement.Text = "replacement text "
    .Forward = True
    .Wrap = 1
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  'the Replace argument is the 11'th argument
    .Execute , , , , , , , , , , wdReplaceAll
  End With
End Sub

Based on my testing with Word 2007 (under Crossover FWIW), I think this is occurring when the "Find" text (Find.Text) contains invalid characters for the Find.MatchWildcards flag. That flag is sticky, so if the user happened to use it on their last manual find or find/replace the VBA script would be contaminated with that sticky setting, resulting in the runtime error 5692 when you use characters such as '^p' and '^t' in your Find.Text string. Explicitly setting "Find.MatchWildcards = False" before running Find.Execute prevents the error. You'd think we could have had some documentation or a better error message though.

Test case that always fails:

Sub ReplaceInDocument
  Dim wdReplaceAll
  'set the value for the replace "constant"
  wdReplaceAll = 2
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p"
    .Replacement.Text = "replacement text "
    .Forward = True
    .Wrap = 1
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  'the Replace argument is the 11'th argument
    .Execute , , , , , , , , , , wdReplaceAll
  End With
End Sub

Test case that works:

Sub ReplaceInDocument
  Dim wdReplaceAll
  'set the value for the replace "constant"
  wdReplaceAll = 2
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p"
    .Replacement.Text = "replacement text "
    .Forward = True
    .Wrap = 1
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  'the Replace argument is the 11'th argument
    .Execute , , , , , , , , , , wdReplaceAll
  End With
End Sub
提笔落墨 2024-10-03 16:59:25

我做了一些研究,但是像你一样,我没有找到任何文档。
出现< /a> 该错误有时可能是由于未清除查找/替换文本引起的。

我已经使用几个不同的输入测试了下面的代码,并且它运行没有错误。
注意:代码需要勾选Tools ->参考文献-> Microsoft Word xx.0 对象库。

Sub TextReplace()

    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oFind As Find

    Set oWord = CreateObject("Word.Application")
    oWord.Visible = True
    Set oDoc = oWord.Documents.Add("C:\in.doc")
    Set oFind = oDoc.Range.Find

    oFind.Execute "foo", , , , , , , , , "bar", wdReplaceAll

    oDoc.SaveAs ("C:\out.doc")
    oDoc.Close
    oWord.Visible = False

End Sub

如果运行此代码时出现任何错误,请随时更新您的问题。

I did some research, but—like you—I found no documentation.
It appears that this error can sometimes be caused by not clearing find/replace text.

I have tested the code below with several different inputs, and it is running without errors.
Note: code requires checking Tools -> References -> Microsoft Word xx.0 Object Library.

Sub TextReplace()

    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oFind As Find

    Set oWord = CreateObject("Word.Application")
    oWord.Visible = True
    Set oDoc = oWord.Documents.Add("C:\in.doc")
    Set oFind = oDoc.Range.Find

    oFind.Execute "foo", , , , , , , , , "bar", wdReplaceAll

    oDoc.SaveAs ("C:\out.doc")
    oDoc.Close
    oWord.Visible = False

End Sub

Feel free to update your question with any errors you get while running this code.

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