0
因此,我有一个.txt文件,它是一个特殊字词列表,我希望我的宏读取文本文件并在打开的文档中突出显示第一次出现字符串行1从文本文件中重复,直到文件结束。此代码有效,但它只查找第一个字符串的第一个匹配项,并且找不到任何后续字符串的第一个匹配项。没有错误。我知道这些行正在被读取(取消注释msgbox mystring并显示文本文件中的每个字符串)。我究竟做错了什么?MS Word VBA,突出显示字符串第一次出现的代码
Sub acronym_highlighter()
Dim MyString As String
Dim MyAcroFileName As String
'Ask user for name of file
MyAcroFileName = InputBox("Enter the filename containg the acronyms. This file MUST be .txt format:", vbOKOnly, "Enter file name")
Open MyAcroFileName For Input As #1
'loop through the file until the end of file marker
'is reached
Do While Not EOF(1)
'read line of text, place it in the MyString variable
Line Input #1, MyString
' MsgBox MyString
Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = MyString
.Replacement.Text = MyString
End With
Selection.Find.Execute Replace:=wdReplaceOne
Loop
'close the text file
Close #1
End Sub