我对 VBA 一点也不熟悉。我在网上找到了一个代码,可以让我同时替换几个单词。一年前我找到了它,但我现在找不到它,所以不能把它归功于写它的人(我的意思是,它不是我写的)。
它工作得很好,但是我不想在 VBscript 中输入 StrFind 和 StrRepl 中的所有单词,而是想从包含单词列表和相应替换单词的文本文件中加载单词。
这是代码:
Sub MultiReplace()
Dim StrFind As String, StrRepl As String
Dim i As Long
StrFind = "word1, word2, word3"
StrRepl = "hello1, hello2, hello3"
Set RngTxt = Selection.Range
For i = 0 To UBound(Split(StrFind, ","))
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = Split(StrFind, ",")(i)
.Replacement.Text = Split(StrRepl, ",")(i)
.Format = False
.MatchWholeWord = True
.MatchAllWordForms = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Next i
'
End Sub
我想调整行 StrFind = "word1, word2, word3" StrRepl = "hello1, hello2, hello3" 所以它从文本文件中获取数据,这是因为如果我有很多单词/字母,我会得到一个错误。
这是文本文件的外观:list.txt:
word1 == hello1
word2 == hello2
它不需要是一个 txt 文件,一个 excel 也可以工作(但我想保持在 word 中运行脚本的超快时间)。
感谢帮助