获取word VBA中某个部分的表值



我有一个很大的word文档,在几个不同的部分中有几个表。如下所示:

  • 第一节
    • Section 1.1 (has table)
    • 1.2节
      • Section 1.2.1 (has table)
  • 第二节
    • Section 2.1(有表)
      • Section 2.1.1 (has table)
    • 2.2节
  • 第3节(有表)

但是,我只想把表放在一个节中。是否有一种方法来创建一个循环,可以从所有的表中获取数据只在一个特定的部分?

我正在考虑使用ActiveDocument.TablesOfContents(1)之类的东西,但我认为我需要更多。有没有一种方法可以经常检查你在哪个区域?可能是一些伪代码,比如…

'Starting at the top of the document
'This would not start until the document found Section 2, and would end once it found section 3
While [ActiveDocument.TablesOfContents("name")]    
'Which would include 2.1, 2.1.1, and 2.2 because they are a part of 2 
TableCount = ActiveDocument.Tables.Count   'But only header 2
For tableNum = 1 To TableCount
' do something/get data
Next tableNum
Wend

你需要做的第一件事是找到标题1样式的段落,它标志着你想要查看的范围的开始。然后,您可以使用文档的GoTo方法转到预定义的书签,该书签将为您提供标题级别的整个范围,从标题1段开始并扩展到下一个标题1段。然后您可以访问该Range

Tables

集合我现在无法访问PC来给你代码,但是这里有很多例子,例如Word VBA:如何使用标题从模板文档中删除文本部分(纯属巧合,它是我的一个!)

最新更新