VBA宏: Which table my cursor is in?

标签 vba ms-word

编写宏时,我发现我需要跳过表格内容并将光标放在其后,为此我使用的代码为

     Selection.Tables(cnt).Select
     Selection.Collapse WdCollapseDirection.wdCollapseEnd

这里,cnt是一个计数器值,每次找到表时它都会增加,但是如果在选择性页面中运行宏,那么我如何知道光标所在的第n个表的编号。

最佳答案

重要!此解决方案允许您查找文档中当前选定表格的数量。

将此函数添加到您的任何模块:

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

要检查 table 号,您可以这样调用它:

debug.Print WhichTableNo(Selection.Range)

因此,您可以获得当前所在的 table 的编号。

关于VBA宏: Which table my cursor is in?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16647397/

相关文章:

c++ - 如何在 C/C++ 上创建和保存 .docx 文档?

excel - 清除单元格内容

vba - 复制/粘贴错误 特殊粘贴

arrays - Excel VBA Array() 函数导致类型不匹配?

vba - 嵌套集合,访问元素类型不匹配

c# - 使用 Word 2007 作为 CMS 页面编辑器

excel - 从日期中删除存储在数组中的整个数据的时间

vba - 使用邮件合并从 Excel 文件生成 Word 文档

vba - MS Word 函数将范围附加到文档

c# - 在 Excel 中调用 WCF 方法切换 "context"并让 Word 继续 "work"