【问题标题】:VBA Word: Find immediate proceeding headerVBA Word:查找立即进行的标题
【发布时间】:2014-07-09 05:11:27
【问题描述】:

我有一个包含很多表格的 word 文档。我想将每个表映射到它们下面列出的直接标题。现在我正在考虑通过每个单独的表传递选择光标,并以某种方式找到选择光标所在的直接标题。我很难找到标题。我找不到任何记录在案的成员函数可以帮助我做到这一点。

For Each T In wrdDoc.Tables
wrdApp.Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext
'Find heading
Next T

编辑 澄清文档的格式:

1

表 1 表 2

1.1

表 3

1.1.1

表 4

1.2

表 5

2

2.1

表 6

基本上,标题有多个级别。在每个之下,可能有也可能没有多个表。所以在上面的例子中,我想想办法把表 1 和 2 映射到 1,表 3 到 1.1,表 4 到 1.1.1 等等。

【问题讨论】:

  • 您的问题不清楚 - 什么在哪里?,您所说的直接标题是什么意思?等等...?我对您的最佳想法是添加屏幕截图或带有标题的表格方案。
  • word.mvps.org/faqs/numbering/liststring.htm 你必须向左移动直到你到达第一段。

标签: vba ms-word


【解决方案1】:

如果您的标题是列表段落,那么您可以使用以下解决方案:

Sub Under_Table_Numbered_List_Item()

    Dim TBL As Table
    For Each TBL In ActiveDocument.Tables

        'get first list item below each table
        'select it- not required
        ActiveDocument.Range(TBL.Range.End).ListParagraphs(1).Range.Select

        'read its text
        Debug.Print ActiveDocument.Range(TBL.Range.End).ListParagraphs(1).Range.Text

    Next

End Sub

但是,如果您想在每个标题为样式的表格下方找到第一个标题,请尝试使用以下代码:

Sub Under_Table_Heading_style()

    Dim TBL As Table
    Dim Para As Paragraph
    For Each TBL In ActiveDocument.Tables

        'get first heading below table
        'based on style name
        For Each Para In ActiveDocument.Range(TBL.Range.End).Paragraphs
            Para.Range.Select

            If Para.Range.Style = "Heading 1" Then
                    'this is your heading
                    Debug.Print Para.Range.Text
            End If

            'stop searchin if you reach next table
            If Para.Range.Tables.Count > 0 Then Exit For
        Next
    Next

End Sub

【讨论】:

    【解决方案2】:

    我认为这可以满足您的需求(Word 2003)。

    Option Explicit
    
    Sub DC1() ' find immediate preceeding header for all tables
      Dim tblnum&, swnone&
      For tblnum = 1 To ActiveDocument.Tables.Count
        swnone = 0 ' to detect "before all numbered paragraphs"
        ActiveDocument.Tables(tblnum).Cell(1, 1).Select
        Do ' get out of table
          If Selection.MoveLeft(wdCharacter, 1) = 0 Then swnone = 1: Exit Do
        Loop While Selection.Information(wdWithInTable)
        Do ' find previous numbered paragraph
          If Selection.Paragraphs(1).Range.ListParagraphs.Count = 1 Then Exit Do
          If Selection.MoveLeft(wdCharacter, 1) = 0 Then swnone = 1: Exit Do
        Loop
        If swnone Then
          Debug.Print "Tbl#" & tblnum, "Before first numbered paragraph"
        Else
          Debug.Print "Tbl#" & tblnum, Selection.Paragraphs(1).Range.ListFormat.ListString
        End If
      Next tblnum
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-06
      • 2013-11-09
      • 2012-03-08
      • 1970-01-01
      • 2021-05-25
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多