如何从MS Word文档中选择性提取以黄色突出显示的文本? [英] How to perform a selective extraction of text highlighted in yellow from an MS Word document?

查看:1221
本文介绍了如何从MS Word文档中选择性提取以黄色突出显示的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个VB脚本,该脚本从给定的MS Word文档中提取以黄色突出显示的所有文本段落.我的代码似乎>>几乎<<可以正常工作...但是我无法将文本导出限制为仅以黄色突出显示的部分.请注意,如果文档包含多种颜色的突出显示,则脚本必须对突出显示颜色具有选择性.

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

推荐答案

这可能有帮助

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

这篇关于如何从MS Word文档中选择性提取以黄色突出显示的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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