Find.Execute 与确认对话框 [英] Find.Execute with confirmation dialog

查看:133
本文介绍了Find.Execute 与确认对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Visual Basic 编写一个宏(呃,我知道)来解析 Microsoft Word 中的文档.这是我试图实现的工作流程:

I'm writing a macro in Visual Basic (ugh, I know) to parse documents in Microsoft Word. This is the workflow I'm trying to achieve:

  • 在文档中搜索字符串(相当于Edit > Find > Find...).
  • 询问用户是否想用另一个字符串替换匹配的字符串(相当于Edit > Find > Replace... > Replace,但在执行之前会出现一个确认对话框替换).
  • 如果是,请更换.如果没有,请转到下一场比赛.
  • Search for a string in the document (equivalent to Edit > Find > Find...).
  • Ask the user if he or she wants to replace the matching string with another string (equivalent to Edit > Find > Replace... > Replace, but with a confirmation dialog before performing the replacement).
  • If yes, do the replacement. If not, go to the next match.

我可以找到并替换为 Find.Execute 方法:

I can do the finding and replacing with the Find.Execute method:

Set myRange = ActiveDocument.Content 
myRange.Find.Execute FindText:="hi", _ 
    ReplaceWith:="hello", Replace:=wdReplaceAll

但我不确定如何在执行替换之前提示用户.

But I'm not sure how to prompt the user before performing the replacement.

推荐答案

你可以用一个消息框提示,然后测试返回值并在此基础上进行替换:

You can prompt with a message box, then test the return value and perform the replacement based on that:

Private Sub PromptForReplace()

    Dim myRange As Range

    Set myRange = ActiveDocument.Content
    myRange.Find.ClearFormatting
    myRange.Find.MatchWildcards = True

    Dim cached As Long
    cached = myRange.End
    Do While myRange.Find.Execute("hi")
        myRange.Select
        If MsgBox("Replace " & myRange.Find.Text & "?", vbYesNo) = vbYes Then
            myRange.Text = "hello"
        End If
        myRange.Start = myRange.Start + Len(myRange.Find.Text)
        myRange.End = cached
    Loop

End Sub

这篇关于Find.Execute 与确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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