MS Word - 查找数字,而不是单个数字

MS Word - 查找数字,而不是单个数字

下面的代码搜索 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

相关内容