怎样在word VBA中选取某几段第一行?

比如:
1.A
aaaa
2.B
bbbb
3.C
cccc

最后希望仅留下
1.A
2.B
3.C

先得到Selection,再得到paragraph用Left函数提取

p = ActiveDocument.Paragraphs.Count
for i= i to p
ActiveDocument.Paragraphs(i).Range.Select
SELECTION.MoveStart unit:=wdLine, Count:=1 '仅留下第一行
SELECTION.Delete
next i

'Hope help,by the way, I need a little C币 -_-...

解决了,看图

img


下面是代码,直接全部复制就行

Sub 取每个段落第一行()
    Dim p, i, duanluoCount As Integer
    
    '段落的数量
    duanluoCount = ActiveDocument.Paragraphs.Count
    
    '建立数组储存找到的第一行
    Dim 第一行(100) As String
    
    For i = 1 To duanluoCount
        p = 查找("^p", False, True) '调用查找函数找到回车
        
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        第一行(i) = Selection.Range.Text
    Next i
    
    i = 1
    Do
    Debug.Print "第" & i & "行:" & 第一行(i)
    i = i + 1
    Loop Until 第一行(i) = ""
    
End Sub
Function 查找(文本, 通配符, 向下)

    Selection.Find.Font.Reset
    Selection.Find.ParagraphFormat.Reset
    With Selection.Find
        .Text = 文本
        .Forward = 向下
        .Wrap = wdFindContinue
        .MatchCase = True
        .MatchByte = True
        .MatchWildcards = 通配符
        .MatchWholeWord = False
        .MatchFuzzy = False
        .Replacement.Text = ""
    End With
    With Selection.Find
        .Style = ""
        .Highlight = wdUndefined
        With .Replacement
            .Style = ""
            .Highlight = wdUndefined
        End With
    End With
    Selection.Find.Execute Replace:=wdReplaceNone
    Selection.Find.Replacement.Text = ""
End Function