VBA新手,从word到excel提取突出显示的单词

时间:2013-11-07 14:47:15

标签: excel excel-vba ms-word export-to-excel word-vba vba

VBA全新。有没有人有代码示例可以从Microsoft Word中提取突出显示的单词到excel模板?

已创建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

1 个答案:

答案 0 :(得分:1)

因此,运行代码后,您需要运行下面的代码,将所有突出显示的单词转移到您的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