高亮显示MS Word文件中的列表单词 [英] Highlight list words in a MS Word file

查看:48
本文介绍了高亮显示MS Word文件中的列表单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说的是我对VBA一无所知,我想将A.txt(或.doc)文件中的突出显示单词 到MS-包含文本的Word文件B.docx .我发现一个VBA代码可以很好地运行,但是需要您在代码中输入 StrFnd ="dog,cat,pig,horse,man" .您能以某种方式帮助我更改代码,而不是键入这些单词来获取列表单词文件A.txt.非常感谢.

First I want to say that I don't know anything about VBA and I want to highlight list words which is in A.txt(or .doc) file to a MS-Word file B.docx that full of texts. I found a VBA code that works perfectly fine but It needs you put words into the code StrFnd = "dog,cat,pig,horse,man". Can you help me change it somehow the code gets list words file A.txt instead of typing these words. Thank you very much.

Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
StrFnd = "dog,cat,pig,horse,man"
For i = 0 To UBound(Split(StrFnd, ","))
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = Split(StrFnd, ",")(i)
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub

推荐答案

好的,这将在第一个文档中查找单词列表,然后在当前文档中突出显示相同的单词.

Ok, this will look at the first document for the list of words, then highlight the same words in the current document.

Sub CompareWordList()
  Dim sCheckDoc As String
  Dim docRef As Document
  Dim docCurrent As Document
  Dim wrdRef As Object

sCheckDoc = "C:\highlight\A.txt"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate

With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Font.Bold = True
    .Replacement.Text = "^&"
    .Forward = True
    .Format = True
    .MatchWholeWord = True
    .MatchCase = True
    .MatchWildcards = False
End With

For Each wrdRef In docRef.Words
    If Asc(Left(wrdRef, 1)) > 32 Then
        With Selection.Find
            .Wrap = wdFindContinue
            .Text = wrdRef
            .Execute Replace:=wdReplaceAll
        End With
    End If
Next wrdRef

docRef.Close
docCurrent.Activate
End Sub

这篇关于高亮显示MS Word文件中的列表单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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