2014-11-05 55 views
0

我有一個單詞文檔,我希望選擇以枚舉2.3.1開頭的標題全文,直到(不包括)標題2.3.2或[文件結束] 。如果在兩者之間存在「較小」的小節或圖片或表格,則也應該選擇它們。單詞vba:在標題之間選擇文本

PS:例:

... 2.2 Blah Blah 2.3 Blubb Blubb [Start Selection] 2.3.1 Important1 Important2 [Picture: Important3] [Table: Important4] 2.3.1.1 Important 5 Important 6 [Stop Selection] 2.3.2 Blieh

我已經嘗試通過每款導航,但這是相當緩慢。我需要這個功能來複制選擇後(我已經知道如何做到這一點;-))。

非常感謝您的幫助!

1月

回答

1

這似乎工作得很好。
調整格式設置,以便僅在給定的格式類型中找到'2.3.1'等。

Sub Macro1() 
    Selection.WholeStory 
    Selection.Collapse wdCollapseStart 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("Caption 1") 
    With Selection.Find 
     .Text = "2.3.1" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = True 
    End With 
    Selection.Find.Execute 
    Selection.Collapse wdCollapseStart 

    Dim r1 As Range 
    Set r1 = Selection.Range 

    ' keep format settings, only change text 
    Selection.Find.Text = "2.3.2" 
    If Selection.Find.Execute Then 
     Selection.Collapse wdCollapseStart 
    Else 
     Selection.WholeStory 
     Selection.Collapse wdCollapseEnd 
    End If 
    Dim r2 As Range 
    Set r2 = ActiveDocument.Range(r1.Start, Selection.Start) 
    r2.Select 

End Sub 
0

這是我用來選擇標題之間的文本的VBA宏。但是,它只能在任何級別的任何兩個標題之間進行選擇。它不會包含較小的小標題。

Sub SelectBetweenHeadings() 
    With Selection 
     .GoTo What:=wdGoToHeading, Which:=wdGoToPrevious 
     .Collapse 
     Dim curRange As Range 
     Set curRange = .Range 
     .Extend 
     .GoTo What:=wdGoToHeading, Which:=wdGoToNext 
     If .Range = curRange Then 
      .EndKey Unit:=wdStory 
     End If 
     .ExtendMode = False 
    End With 
End Sub