VBA字:查找立即继续标题
我有一个word文档,里面有很多表格。我想将每个表映射到它们所列的直接标题下。现在,我正在考虑将选择游标传递到每个单独的表中,并以某种方式找到选择游标所在的直接标题。我找不到标题。我找不到任何文档化的成员函数可以帮助我做到这一点VBA字:查找立即继续标题,vba,ms-word,Vba,Ms Word,我有一个word文档,里面有很多表格。我想将每个表映射到它们所列的直接标题下。现在,我正在考虑将选择游标传递到每个单独的表中,并以某种方式找到选择游标所在的直接标题。我找不到标题。我找不到任何文档化的成员函数可以帮助我做到这一点 For Each T In wrdDoc.Tables wrdApp.Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext 'Find heading Next T 编辑 要澄清文档的格式,请执行以下操作: 1. 表1
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等等。如果标题是列表段落,则可以使用以下解决方案:
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
我想这正是你想要的(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