|
- Option Explicit
- Sub Test()
- Dim arrList As Variant, strContent As String
- Dim objReg As Object, strTemp As String, strPat As String
- Dim objMatchs As Object, objMatch As Object
- Dim arrResult As Variant, lngID As Long
-
- arrList = GetFileNameList(ThisWorkbook.Path, ".doc*")
- strContent = GetTextFromDoc(arrList)
- Debug.Print strContent
-
- strPat = "([^\.]*)interesting([^\.]*\.)"
- Set objReg = CreateObject("VBScript.RegExp")
- With objReg
- .Global = True
- .Pattern = strPat
- End With
- Set objMatchs = objReg.Execute(strContent)
- If objMatchs.Count = 0 Then Exit Sub
-
- ReDim arrResult(1 To objMatchs.Count, 1 To 1)
- lngID = 1
- For Each objMatch In objMatchs
- strTemp = objMatch
- strTemp = Replace(strTemp, "interesting", String(10, "_"))
- arrResult(lngID, 1) = Trim(strTemp)
- lngID = lngID + 1
- Next
-
- Sheet1.UsedRange.ClearContents
- Sheet1.Range("A1").Resize(UBound(arrResult), 1) = arrResult
- MsgBox "OK"
- End Sub
- '读取WORD 文档中的文字
- Function GetTextFromDoc(arrFileList As Variant) As String
- Dim objWord As Object, objDoc As Object, lngR As Long
- Dim strPath As String, strFileName As String
- Dim strTemp As String
-
- Set objWord = CreateObject("word.application")
-
- For lngR = LBound(arrFileList) To UBound(arrFileList)
- strPath = arrFileList(lngR): strFileName = ""
- If strPath <> "" Then strFileName = Dir(strPath)
- If strFileName <> "" Then
- Set objDoc = objWord.Documents.Open(strPath, True, True)
- strTemp = strTemp & " " & objDoc.Content
- objDoc.Close False
- End If
- Next
-
- objWord.Quit
- Set objDoc = Nothing: Set objWord = Nothing
-
- strTemp = Replace(strTemp, Chr(10), " ")
- strTemp = Replace(strTemp, Chr(13), " ")
-
- GetTextFromDoc = strTemp
- End Function
- '得到当前目录及子目录下,指定后缀的文件列表
- Function GetFileNameList(strPath As String, Optional strFileType As String = "*") As Variant
- Dim strFileList As String
- Dim arrResult As Variant
- With CreateObject("Wscript.Shell")
- strFileList = .exec("cmd /c dir /a-d/b/s/n " & strPath & " | findstr /I ." & strFileType).StdOut.Readall
- End With
- If strFileList = "" Then
- ReDim arrResult(1 To 1)
- arrResult(1) = "False"
- Else
- arrResult = Split(strFileList, vbCrLf)
- End If
- GetFileNameList = arrResult
- End Function
复制代码 |
|