Highlight selective words RRS feed

  • Question

  • Looking for macro/vb6/ code to highlight in yellow certain words (as may be preset in an array), if found,  in word 2007 document.
    Saturday, July 18, 2015 6:50 PM


All replies

    • Marked as answer by L.HlModerator Thursday, July 30, 2015 2:35 AM
    Sunday, July 19, 2015 1:34 AM
  • Following code in VB.NET could find and highlight certain text in Word document.

    Imports System.Drawing
    Imports Spire.Doc
    Imports Spire.Doc.Documents
    Namespace FindAndHighlight
        Friend Class ImageinWord
            Shared Sub Main(ByVal args() As String)
                'Load Word Document
                Dim document As New Document()
                document.LoadFromFile(string fileName)
                Dim text() As TextSelection = document.FindAllString("Selected Text", False, True)
                For Each seletion As TextSelection In text
                    seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow
                Next seletion
                document.SaveToFile("result.docx", FileFormat.Docx)
            End Sub
        End Class
    End Namespace

    This method is based on .NET Word component. Do remember to add the dll in you project.

    • Edited by leo_456 Friday, August 14, 2015 7:52 AM
    Friday, August 14, 2015 7:49 AM
  • This will do it for you.

    Sub HighlightSpecificWords() 
    Dim sArr() As String 
    Dim rTmp As Range 
    Dim x As Long 
    sArr = Split("highlight specific words") ' your list 
    Options.DefaultHighlightColorIndex = wdYellow 
    For x = 0 To UBound(sArr) 
      Set rTmp = ActiveDocument.Range 
      With rTmp.Find 
      .Text = sArr(x) 
      .Replacement.Text = sArr(x) 
      .Replacement.Highlight = True 
      .Execute Replace:=wdReplaceAll 
    End With 
    End Sub

    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Thursday, January 7, 2016 3:19 AM
  • You're just reinventing the wheel - even a cursory glance at the post in the link I posted will show you that you're doing essentially the same thing...

    Paul Edstein
    [MS MVP - Word]

    Thursday, January 7, 2016 7:04 AM