【问题标题】:word vba: select text between headingsword vba:选择标题之间的文本
【发布时间】:2014-11-05 17:51:27
【问题描述】:

我有一个 word 文档,我想在其中选择标题的全文,从枚举 2.3.1 开始,直到(不包括)标题 2.3.2 或 [End of File]。如果中间有“较小”的小节或图片或表格,也应选择它们。

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

我已经尝试过浏览每个段落,但这很慢。之后我需要此功能来复制所选内容(我已经知道该怎么做 ;-))。

非常感谢您的帮助!

一月

【问题讨论】:

    标签: vba ms-word


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      这是我用来选择标题之间文本的 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
      

      【讨论】:

        猜你喜欢
        • 2013-10-29
        • 1970-01-01
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多