“查找并替换"; Excel列表中Word中的多个单词 [英] "Find and Replace" multiple words in Word from Excel list

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

问题描述

我已经在MS Word中创建了一个查找并替换宏,该宏用B替换了单词A.好了,但是现在我有50个单词需要替换.这意味着我将必须为每个单词创建一个新条目,这将永远存在.再过几周后,我将不得不添加更多要替换的单词.

I have created a find and replace Macro in MS Word that replaces word A with B. Ok, but now I have 50 words that need replacing. That means I will have to create a new entry for each word, which will take FOREVER. Plus a few weeks from now I will have to add more words to be replaced.

有没有一种方法可以通过excel链接单词列表,比如说第1列中的单词是我要用第2列中的匹配单词替换的单词?

Is there a way to link a word list via excel, say words in column 1 are the words I want replaced with the matching words in column 2?

这是我到目前为止所拥有的.

Here's what I have so far.

Sub Macro5()
'
' Macro5 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "apples"
        .Replacement.Text = "all the apples"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute
End Sub

推荐答案

类似的东西应该可以帮助您入门.将Excel绑定到Word,打开包含该列表的文件,然后遍历该列表,依次调用您的宏(已修改以接受两个字符串参数findTextreplaceText).

Something like this should get you started. Bind Excel to Word, open the file which contains the list, and iterate over the list, calling your macro (modified to accept two string arguments, findText and replaceText) sequentially.

Sub Main()
Dim xl as Object 'Excel.Application
Dim wb as Object 'Excel.Workbook
Dim ws as Object 'Excel.Worksheet
Dim rng as Object 'Excel.Range
Dim cl as Object  'Excel.Range
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open("c:\folder\file.xlsx") '## Modify as needed
Set ws = wb.Sheets(1) '##Modify as needed
Set rng = ws.Range("A1", ws.Range("A1").End(xlDown))
For each cl in rng
    Call Macro5(cl.Value, cl.offset(0,1).Value)
Next
End Sub

您可以自行确认Macro5的内容是否在上述循环中正常工作.

You are on your own to confirm that the contents of Macro5 works as intended within the above loop.

Sub Macro5(findText$, replaceText$)
'
' Macro5 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = findText
        .Replacement.Text = replaceText
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute
End Sub

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

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