|
Option Explicit
Sub TEST1()
Dim ar, i&, r&, wdApp As Object, strFileName$, strPath$, regEx As Object
Application.ScreenUpdating = False
ReDim ar(1 To 10 ^ 3, 1)
ar(1, 0) = "单位": ar(1, 1) = "资料"
r = 1
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "\d+年资料"
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
strPath = ThisWorkbook.Path & "\"
strFileName = Dir(strPath & "*.doc*")
Do Until strFileName = ""
With wdApp.documents.Open(strPath & strFileName)
r = r + 1
ar(r, 0) = .Range(.Paragraphs(2).Range.Start, .Paragraphs(2).Range.End - 2)
If regEx.test(.Content.Text) Then
ar(r, 1) = regEx.Execute(.Content.Text)(0).Value
End If
.Close False
End With
strFileName = Dir
Loop
Cells.Clear
[A1].Resize(r, 2) = ar
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Set regEx = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
|