[英]Word-vba: Identifying the table hosting a range
我正在嘗試識別范圍的表索引; 或更具體地說:我有一個帶有一些表格的 Word 文檔,每個表格都包含復選框類型的 ContentControls。 我正在循環 ContentControls,如果類型是 CheckBox,那么我想操作表格中的文本,但要做到這一點,我需要知道表格索引。 我可以確定 CheckBox 是否實際上在一個表中(兩種方法之一),並且我可以識別行號和列號,但我還沒有弄清楚如何建立表號。
Dim docActive As Document
Dim ContCtrl As ContentControl
Dim TableNo As Integer
Dim UpperLeftText As String
Set docActive = ActiveDocument
For Each ContCtrl In docActive.ContentControls
If ContCtrl.Type = wdContentControlCheckBox Then
If ContCtrl.Range.Information(wdWithInTable)
MsgBox ("RowNumber: " & ContCtrl.Range.Information(wdEndOfRangeRowNumber))
TableNo = ContCtrl.Range.Information(wdTableNumber)) ' This doesn't work, I know, and this is the line for which I need help
MsgBox ("TableRef: " & TableNo)
UpperLeftText = docActive.Tables(TableNo).Rows(1).Cells(1).Range.Text ' Contents of upper left cell assigned to variable UpperLeftText
End If
End If
Next
有任何想法嗎?
是的,我可以計算出表格的數量,然后遍歷這些表格,這將是一個已知的數字,但這並不是很優雅......
您無需使用文檔表集合中的索引來訪問內容控件所在的表。 您可以簡單地使用內容控件范圍的 Tables 集合,即
UpperLeftText = ContCtrl.Range.Tables(1).Rows(1).Cells(1).Range.Text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.