首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA -查找以数字开头的段落

问VBA -查找以数字开头的段落
EN

Stack Overflow用户
提问于 2020-03-18 20:23:47
回答 2查看 354关注 0票数 0

我正在使用一个VBA脚本来尝试查找段落的起始编号(它们是没有格式的列表项-不是试图格式化,而是查找编号)。

代码语言:javascript
复制
1. First Item
2. Second Item
No number - don't include despite 61.5 in paragraph.
25 elephants should not be included
12. Item Twelve, but don't duplicate because of Susie's 35 items

有没有办法在VBA中说“如果段落开头有1-2个数字,则返回这些数字”。在正则表达式中,我要查找的是^(\d\+)\.

这是一段有效的VBA代码--还没有想出如何创建excel文件,所以如果你去测试,在你的临时文件夹中创建一个空白的test.xslx。当然,这可能足够简单,测试是不必要的。

代码语言:javascript
复制
Sub FindWordCopySentence()
On Error Resume Next
Dim appExcel As Object
Dim objSheet As Object
Dim aRange As Range
Dim intRowCount As Integer
intRowCount = 1

' Open Excel File
If objSheet Is Nothing Then
    Set appExcel = CreateObject("Excel.Application")
     'Change the file path to match the location of your test.xls
    Set objSheet = appExcel.workbooks.Open("C:\temp\test.xlsx").Sheets("Sheet1")
    intRowCount = 1
End If

' Word Document Find
Set aRange = ActiveDocument.Range
With aRange.Find
    Do
        .ClearFormatting
        ' Find 1-2 digit number
        .Text = "[0-9]{1,2}"
        .MatchWildcards = True
        .Execute
        If .Found Then
            ' Copy to Excel file
            aRange.Expand Unit:=wdSentence
            aRange.Copy
            aRange.Collapse wdCollapseEnd
            objSheet.Cells(intRowCount, 1).Select
            objSheet.Paste
            intRowCount = intRowCount + 1
        End If
    Loop While .Found
End With
Set aRange = Nothing

If Not objSheet Is Nothing Then
    appExcel.workbooks(1).Close True
    appExcel.Quit
    Set objSheet = Nothing
    Set appExcel = Nothing
End If

End Sub

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-18 21:49:50

我会简单得多,只检查段落的前几个字符:

代码语言:javascript
复制
Option Explicit

Sub test()
    Dim para As Paragraph
    For Each para In ThisDocument.Paragraphs
        With para.Range
            If (.Characters(2) = ".") Or (.Characters(3) = ".") Then
                If IsNumeric(para.Range.Words(1)) Then
                    Debug.Print "Do something with paragraph number " & _
                                 para.Range.Words(1) & "."
                End If
            End If
        End With
    Next para
End Sub
票数 1
EN

Stack Overflow用户

发布于 2020-03-19 06:16:00

一种更有效的方法,它避免了测试每个段落的需要:

代码语言:javascript
复制
Sub Demo()
Application.ScreenUpdating = False
Dim StrOut As String
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^13[0-9.]{1,}" ' or: .Text = "^13[0-9]{1,}
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    StrOut = StrOut & .Text
    ' or: MsgBox Split(.Text, vbCr)(1)
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
MsgBox StrOut
End Sub

按照编码,宏会返回整个列表字符串,其中可能有多个级别(例如1.2)。注释显示了如何仅查找可能有多个级别的第一个数字,以及如何提取该数字以进行测试(查找表达式包括前面的分段符)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60739514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档