在 Word 中查找和替换多个单词 [英] Find and replace multiple words in Word

查看:120
本文介绍了在 Word 中查找和替换多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望你们能提供帮助,我觉得我一直在努力解决应该(并且可能是)一个非常简单的问题.我编写了一个应用程序,它创建一个文件夹结构,根据用户输入复制相关的测试文档,并用各种变量填充文档标题和测试表.我已经用占位符(replCustNo"、replPrjNo"、replCustRef"等)填充了模板测试文档,一些在标题中,一些在文档正文中.我似乎一次只能替换一个单词,我找不到一种方法列出所有要查找的引用,然后列出所有替换变量.似乎是一种非常笨拙的编码方式,一遍又一遍地查找/替换,或者为它调用一个子程序.

I really hope you guys can help, I feel like I've been struggling with what should be (and probably is) a really simple problem. I've written an app which creates a folder structure, copies in the relevant test documents based on user inputs, and populates the document header and test sheets with various variables. I have populated the template test documents with placeholders, ("replCustNo","replPrjNo","replCustRef" etc), some in the header, some in the body of the document. I can only seem to replace one word at a time, I can't find a way of listing all the references to find, then listing all the replacement variables. Seems like a very clumsy way of coding to have the find/replace over and over, or call on a sub for it.

请原谅我,如果这真的很基础,我对编码很陌生,我真的很感激一些帮助!我正在使用 vb 和 office 365

Forgive me if this is really basic, I'm very new to coding, I'd really appreciate some help! I'm using vb and office 365

 Dim objWordApp As New Word.Application
    'Open an existing document.  
    Dim objDoc As Word.Document = objWordApp.Documents.Open(projFolder & "SAT\2 HV Tests\Flash.doc")

    objWordApp.ActiveDocument.Sections(1).Headers(1).Range.Select()
    objWordApp.Selection.WholeStory()
    With objWordApp.Selection.Find
        .Text = "replPrjNo"
        .Replacement.Text = RefNo
        .Forward = True
    End With
    objWordApp.Selection.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)

    'Save and close the document  
    objDoc.Save()
    objDoc.Close()
    objDoc = Nothing
    objWordApp.Quit()
    objWordApp = Nothing

推荐答案

一种解决方案是使用文档变量.在模板文档中选择一个占位符,然后 Insert tab >快速零件 >场... >文档变量 >新名称:replPrjNo

One solution can be with document variables. Select a placeholder in the template document and then Insert tab > Quick Parts > Field... > DocVariable > New name: replPrjNo

然后在代码中:

objDoc.Variables("replPrjNo").Value = RefNo
objDoc.Fields.Update

其他解决方案可以是书签、自定义文档属性、邮件合并或开发人员"选项卡中的某些控件.

Other solutions can be bookmarks, custom document properties, Mail Merge, or some of the controls in the Developer tab.

这篇关于在 Word 中查找和替换多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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