下面的程式碼搜尋 MS Word 文件、提取數字並將其放入 Excel 中。
我需要的是,如果文件有一個像 12345 這樣的數字,它需要提取 12345 而不是 1、2、3、4 和 5。
我意識到這取決於.Text = "[0-9]"
我缺乏正規表達式,但希望有人能提供幫助。
Public Sub NumbersToExcel()
Dim xlApp As Object
Dim xlWbk As Object
Dim xlWsh As Object
Dim blnStartExcel As Boolean
Dim i As Integer
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Cannot activate Excel!", vbExclamation
Exit Sub
End If
blnStartExcel = True
End If
On Error GoTo ErrHandler
Set xlWbk = xlApp.Workbooks.Add
Set xlWsh = xlWbk.Worksheets(1)
With ActiveDocument.Content
With .Find
.ClearFormatting
.Text = "[0-9]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
While .Find.Execute
i = i + 1
xlWsh.Cells(i, 1) = "'" & .Text
Wend
.Find.MatchWildcards = False
End With
ExitHandler:
On Error Resume Next
xlWbk.Close SaveChanges:=True
If blnStartExcel Then
xlApp.Quit
End If
Set xlWsh = Nothing
Set xlWbk = Nothing
Set xlApp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub