|
Sub shishi()
Dim doc As Document, p As Range, s As Range, f1$, f2$
Dim strarr(1 To 1000, 1 To 3), mt, reg As Object
Set doc = ActiveDocument
Set p = doc.Content: rg = "[〇一二三四五六七八九十百千万]"
Set reg = CreateObject("vbscript.regexp")
reg.Global = True: reg.MultiLine = True
reg.Pattern = "^第" & rg & "+条(?:(?!^第" & rg & "+条).)+"
f1 = "标题 1": f2 = "副标题"
a = Spa(doc, p, f1)
For i = 0 To UBound(a, 2)
S1 = a(1, i): Set s = a(0, i)
b = Spa(doc, s, f2)
For j = 0 To UBound(b, 2)
S2 = b(1, j): sr = b(0, j)
For Each mt In reg.Execute(sr)
x = x + 1
strarr(x, 1) = S1
strarr(x, 2) = S2
strarr(x, 3) = mt
Next
Next
Next
If Tasks.Exists("Microsoft Excel") Then
Set xlapp = GetObject(, "excel.application")
Else
Set xlapp = CreateObject("Excel.Application")
End If
Set myBook = xlapp.Workbooks.Add: xlapp.Visible = True
Set mysheet = myBook.Worksheets("sheet1"): mysheet.Activate
mysheet.Range("a1:c1") = Array("文档名", "第几章", "第几条")
mysheet.Range("a2").Resize(x, 3) = strarr
End Sub
Function Spa(doc As Document, p As Range, fr As String)
Dim myStart&, n&, arr(), s As Range
Set s = p.Duplicate
With s.Find
.Style = fr
Do While .Execute
If Not s.InRange(p) Then Exit Do
n = n + 1
ReDim Preserve arr(1, n - 1)
With s
If n > 1 Then
Set arr(0, n - 2) = doc.Range(myStart, .Start)
Set arr(1, n - 2) = doc.Range(myStart, .Start).Paragraphs(1).Range
End If
myStart = .Start: .SetRange .End, .End
End With
Loop
If n > 0 Then
Set arr(0, n - 1) = doc.Range(myStart, p.End)
Set arr(1, n - 1) = doc.Range(myStart, p.End).Paragraphs(1).Range
End If
End With
Spa = arr
End Function |
|