将Word文档的一部分导出为单独的MS Excel单元格VBA

时间:2017-12-14 12:54:23

标签: excel vba ms-word word-vba

我正在尝试在单独的EXCEL单元格中导出Word文档句子。 Basicaly我想通过一个特定的分隔符(任意)分离WORD文档的不同部分,并将分离的句子导入EXCEL的单独单元格中。

Sub TheBoldAndTheExcelful()
Dim docCur  As Document
Dim snt     As Word.Range
Dim i       As Integer
Dim j       As Integer
Set snt = ActiveDocument.Range
'Requires a reference to the 'Microsoft Excel XX.0 Object Library'
Dim appXL As Excel.Application, xlWB As Excel.Workbook, xlWS As Excel.Worksheet

'This assumes excel is currently closed
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
Set xlWB = appXL.Workbooks.Add
Set xlWS = xlWB.Worksheets(1)
j = 1
On Error GoTo ErrHandler
Application.ScreenUpdating = False

Set docCur = ActiveDocument.Range

For Each snt In docCur.Sentences
    With snt.Find
        .Text = "Technical Indicators and Signals"

        While .Execute
            j = j + 1
            xlWS.Cells(j, i).Value = snt.Text
        Wend
    End With
Next snt

ExitHandler:
Application.ScreenUpdating = True
Set snt = Nothing
Exit Sub

ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
  

类型不匹配错误

执行上述代码时遇到

0 个答案:

没有答案