How to perform a selective extraction of text highlighted in yellow from an MS Word document?

大憨熊 提交于 2019-12-12 02:17:15

问题


I am trying to write a VB script that extracts all text passages highlighted in yellow from a given MS Word document. My code seems >>almost<< to be working ... but I have not been able to restrict the text export to sections highlighted in yellow only. Please note the script has to be selective for highlight colour in case the document contains highlights in several colours.

Sub FindHighlightedText()

    'Get current working directory.
    Dim pwd As String
    pwd = ActiveDocument.Path

    Dim Name As String
    Name = pwd & "\Output.txt"

    ' Create a filehandle and open the output file.
    handle = FreeFile()
    Open Name For Output As #handle

    With ActiveDocument.Range.Find

        .ClearFormatting
        .Highlight = True
        .Forward = True

        'I THINK THE PROBLEM IS HERE!!
        If .Parent.HighlightColorIndex = wdYellow Then
              print #handle, .Parent.Text & vbNewLine
        End If

    End With


    ' Close the output filehandle.
    Close #handle

End Sub

回答1:


This might help

Sub Macro1()

    With Selection.Find
        .Highlight = True
        .Wrap = wdFindContinue
        .Format = True
    End With

    Do While Selection.Find.Execute()

        If Selection.Range.HighlightColorIndex = wdYellow Then
             Debug.Print Selection.Range.Text
        End If

    Loop

End Sub


来源:https://stackoverflow.com/questions/15075067/how-to-perform-a-selective-extraction-of-text-highlighted-in-yellow-from-an-ms-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!