|
本帖最后由 duquancai 于 2016-6-25 13:00 编辑
请把以下代码放入需要提取的Word文档VBE中执行,请测试:
Sub WordtoExdel()
Dim strRng As Range
Dim myBook As Object
Dim mysheet As Object
Dim RegMatch
Dim n As Long
Dim arrData()
With CreateObject("VBScript.Regexp")
Set strRng = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
.Global = True
.Pattern = "([一-﨩]{2,5})(?=;[男女])[^\r]+(\d{11})(?!\d)"
ReDim arrData(1 To .Execute(strRng).Count, 1 To 2)
For Each RegMatch In .Execute(strRng)
n = n + 1
arrData(n, 1) = RegMatch.submatches(0)
arrData(n, 2) = "'" & RegMatch.submatches(1)
Next
End With
With CreateObject("Excel.Application")
If Tasks.Exists("Microsoft Excel") = True Then Tasks("Microsoft Excel").Close
Set myBook = .Workbooks.Add: .Visible = True
Set mysheet = myBook.Worksheets("sheet1"): mysheet.Activate
mysheet.Range("a1:b1") = Array("姓名", "手机号码")
mysheet.Range("a2").Resize(n, 2) = arrData
End With
Set RegMatch = Nothing: Set strRng = Nothing
Set myBook = Nothing: Set mysheet = Nothing
End Sub |
|