Macro to Extract Highlighted Words from MS Word file using VBA

MACRO ::: Tested working fine for MS Word 2007

 

 

 

Sub ExtractHighlight()

 

    Dim objWord As Word.Application

    Dim doc As Word.Document

    Dim oneword

    Dim i As Integer

    i = 0

 

    ‘Create Word doc object

    Set objWord = CreateObject(“Word.Application”)

 

    With objWord

        ‘ Ensure the MS Word object is visible

        .Visible = True

 

        ‘ Add a new word document and save the file prior to adding text

        Set doc = .Documents.Add

        doc.SaveAs “C:\test\output.docx”

 

        ‘ Or open an existing document

        ‘Set doc = wrdApp.Documents.Open(“C:\Foldername\Filename.doc”)

    End With

 

    ‘Construct document

    With objWord.Selection

        ‘ Set the font type

        .Font.Name = “Trebuchet MS”

        ‘ Set the font size

        .Font.Size = 16

 

        ‘ Set the format, depending on the value of i

       

 

           ‘ Add text

           

For Each char In ActiveDocument.Characters

           

        If char.HighlightColorIndex = wdYellow Then

                  

       i = 1

        oneword = char.Text

        

 

‘oneword = oneword & “##”

        .TypeText oneword

         oneword = “”

         Else

         If i = 1 Then

         .TypeText vbCrLf

         i = 0

         End If

        

        End If

   

Next

 

    End With

 

    ‘ Save the file

    doc.Save

    ‘ Bring the MS Word window to the front

    doc.Activate

 

 

End Sub

 

 

 

Source File  :

 

Output  File :

 

 

Source Code Screen :

.