vba 宏:我的光标在哪个表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16647397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vba macro: Which table my cursor is in?
提问by Sanghita
Writing a macro I found out that I need to skip the table contents and place my cursor right after that, for this I am using a code as
写一个宏我发现我需要跳过表格内容并将光标放在后面,为此我使用了一个代码
Selection.Tables(cnt).Select
Selection.Collapse WdCollapseDirection.wdCollapseEnd
Here, cnt is a counter value which increases each time a table is found, but if run the macro in selective pages then how will i know the number of nth table inside which my cursor is.
在这里,cnt 是一个计数器值,每次找到表时都会增加,但是如果在选择性页面中运行宏,那么我将如何知道我的光标所在的第 n 个表的数量。
回答by Kazimierz Jawor
Important! This solution allows you to find the number of currently selected table within document.
重要的!此解决方案允许您在文档中查找当前选定表格的数量。
Add this function to any your Module:
将此功能添加到您的任何模块:
Function WhichTableNo(RNG As Range)
If RNG.Tables.Count > 0 Then
Dim DOC As Document
Set DOC = RNG.Parent
Dim rngTMP As Range
Set rngTMP = DOC.Range(0, RNG.Tables(1).Range.End)
WhichTableNo = rngTMP.Tables.Count
Else
WhichTableNo = "Not in the table"
End If
End Function
and to check the table number you could call it this way:
并检查表号,你可以这样称呼它:
debug.Print WhichTableNo(Selection.Range)
As a result you get number of table you are currently in.
因此,您将获得当前所在的表数。
回答by GSerg
The table in which your cursor is is always Selection.Tables(1)
.
光标所在的表始终为Selection.Tables(1)
。
If Selection.Tables.Count > 0 Then
Dim r As Range
Set r = Selection.Tables(1).Range
r.Collapse wdCollapseEnd
r.Select
End If
In case of nested tables, you might want to also check Selection.Tables.NestingLevel
. The following will exit any number of nested tables, placing the cursor after the outermost table:
如果是嵌套表,您可能还想检查Selection.Tables.NestingLevel
. 以下将退出任意数量的嵌套表,将光标放在最外面的表之后:
If Selection.Tables.Count > 0 Then
Dim r As Range, i As Long
Set r = Selection.Range
For i = 1 To r.Tables.NestingLevel
Set r = r.Tables(1).Range
r.Collapse wdCollapseEnd
Next
r.Select
End If