【发布时间】:2018-07-05 18:01:42
【问题描述】:
我想将所有表格移到文档末尾,以便在单独的会话中首先处理所有其他非表格(普通)段落。这是我(最终)想出的代码:
Dim iCounter As Integer
For iCounter = 1 To ThisDocument.Tables.Count
ThisDocument.Tables(1).Range.Select
Selection.Cut
Selection.EndKey (wdStory)
Selection.TypeParagraph
Selection.Paste
Next iCounter
它已经奏效了。但是,我想知道:为什么我总是要操作第一张桌子,而不是第一张,第二张……等等直到最后一张?这种“不断变化的指标”或“不变的地方应该改变”现象的总称或一般概念是什么?为什么像下面这样的普通循环不起作用?
for each oTable in ThisDocument.Tables
oTable.Range.Cut
Selection.EndKey (wdStory)
Selection.TypeParagraph
Selection.Paste
DoEvents
next oTable
上面的解决方案,一个看似正常的循环,结果不正确并最终不间断地运行。我不得不强制关闭 Word 窗口。并且:
Dim iCounter as Integer
For iCounter = 1 To ThisDocument.Tables.Count
ThisDocument.Tables(iCounter).Range.Cut
Selection.EndKey (wdStory)
Selection.TypeParagraph
Selection.Paste
Next iCounter
上述解决方案,另一个看似正常的循环,在试运行时,会输出一个“半成品”,即它移动了一些而不是全部的桌子。
【问题讨论】:
-
索引是按位置移动的,所以当你移动第一个表时,下一个表会变成第一个?
标签: arrays vba indexing dynamic static