0

如何将 Microsoft Word 中突出显示的单词提取到 Excel 模板中?

模板已创建。我希望将 Word 文档中的文本放入已有的 Excel 文档中。

'code that highlights word "olympics"

Dim sFindText As String

sFindText = "Olympics"

Selection.ClearFormatting
Selection.HomeKey wdStory, wdMove
Selection.Find.ClearFormatting
Selection.Find.Execute sFindText

Do Until Selection.Find.Found = False
    Selection.Range.HighlightColorIndex = wdYellow
    Selection.MoveRight
    Selection.Find.Execute
Loop
4

1 回答 1

2

因此,在运行代码后,您需要运行下面的代码将所有突出显示的单词传输到您的 Excel 文件中。请参阅建议的解决方案中的其他评论。

Selection.ClearFormatting
Selection.HomeKey wdStory, wdMove
Selection.Find.ClearFormatting

'here you set searching for highlighted words
Selection.Find.Highlight = True
Selection.Find.Execute

'lets open your workbook within new Excel application
    Dim EXL As Object
    Set EXL = CreateObject("Excel.Application")
    Dim xlsWB As Object 'which will be a workbook

    Dim xlsPath As String
    'put path to your file here
    xlsPath = "c:\Temp Priv\TestFile.xlsm"

    Set xlsWB = EXL.workbooks.Open(xlsPath)

    Dim xlsRow As Long

Do Until Selection.Find.Found = False

        'we will write found words to first sheet in your Workbook,  _
         consecutive rows in column A

        xlsRow = xlsRow + 1
        xlsWB.sheets(1).Cells(xlsRow, "A") = Selection.Text
        Selection.Find.Execute

Loop
'lets show our excel application
    EXL.Visible = True
于 2013-11-07T17:58:29.210 回答