遍历每一个PARAGRAPH对象,判断其STYLE属性,如果是指定的标题格式(如"标题 1"),则其PARAGRAPH对象的RANGE属性中的TEXT属性即为标题,其Range属性的ListFormat属性中的ListString属性为其前面的标题编号.示例如下所示:
Sub temp()
'初始化EXCEL对象
Dim excelapp As Excel.Application
Set excelapp = CreateObject("Excel.Application")
excelapp.Visible = True
excelapp.Workbooks.Add
'将各级标题依次填入EXCEL表
Dim par As Paragraph
Dim i As Integer
i = 1
For Each par In ActiveDocument.Paragraphs
If par.Style = "标题 1" Then
excelapp.ActiveSheet.Range("A" & i) = par.Range.ListFormat.ListString & par.Range.Text
i = i + 1
ElseIf par.Style = "标题 2" Then
excelapp.ActiveSheet.Range("B" & i) = par.Range.ListFormat.ListString & par.Range.Text
i = i + 1
ElseIf par.Style = "标题 3" Then
excelapp.ActiveSheet.Range("C" & i) = par.Range.ListFormat.ListString & par.Range.Text
i = i + 1
End If
Next
End Sub
上述程序将WORD中的三级标题依次提取至EXCEL当前sheet的前三列中。