我有一个包含很多表的word文档。我想将每个表映射到它们列在下面的直接标题。现在我正在考虑通过选择光标遍历每个单独的表,并以某种方式找到选择光标所在的直接标题。我找到标题时遇到了麻烦。我找不到任何可以帮助我做到这一点的成员函数。
For Each T In wrdDoc.Tables
wrdApp.Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext
'Find heading
Next T
修改的 澄清文档的格式:
表1 表2
表3
表4
表5
表6
基本上,标题有多个级别。在每个表格下,可能有也可能没有多个表格。所以在上面的例子中,我想找出一种方法将表1和2映射到1,表3到1.1,表4到1.1.1等等。
答案 0 :(得分: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
答案 1 :(得分:0)
我认为这样做符合你的要求(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