vba 从 Excel 列表中“查找和替换”Word 中的多个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23408568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Find and Replace" multiple words in Word from Excel list
提问by Birk
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.
我在 MS Word 中创建了一个查找和替换宏,用 B 替换单词 A。好的,但现在我有 50 个单词需要替换。这意味着我必须为每个单词创建一个新条目,这将永远持续下去。再加上几周后,我将不得不添加更多要替换的单词。
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?
有没有办法通过excel链接单词列表,比如说第1列中的单词是我想用第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
回答by RubberDuck
- Revise your macro so that it can accept parametersfor "word to find" and "word to replace with".
- Loop through a rangein excel, passing the value of each cellto your revised macro(subroutine).
回答by David Zemens
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.
像这样的事情应该让你开始。将 Excel 绑定到 Word,打开包含列表的文件,并遍历列表,依次调用您的宏(修改为接受两个字符串参数findText
和replaceText
)。
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
You are on your own to confirm that the contents of Macro5
works as intended within the above loop.
您自行确认Macro5
上述循环中的内容是否按预期工作。
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