如何循环现有的宏?

How do I loop an existing macro?

我在 MS Word 中有一个 2 列乘 1,000 行的 table。我制作了一个宏,按顺序

  1. 在当前行下方添加一个新行,
  2. 合并新行中的两个单元格,
  3. 将上述单元格中的文本剪切并粘贴到新行中,
  4. 将图像移到一列上,
  5. 向单元格添加文本。一旦它完成了这一系列事件,光标就可以再次完成这一切。

没有任何条件可以搞砸它 运行s(即空单元格等),我不需要代码来查找特定的东西,我只需要它重复。

我需要向这个现有代码添加什么才能使这个重复直到文档结束(具体是 1,000 次)。

我看到了 Excel 的代码,但我还没有看到在 Word 中遍历 table 的代码。

Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
Selection.InsertRowsBelow 1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
Selection.Cells.Merge
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.Font.Size = 4
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.PasteAndFormat (wdPasteDefault)
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Font.Size = 3
Selection.TypeText Text:="Unique specimen identifier not a property tag"
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1

我期望的是能够 运行 代码一次并重复它直到它到达文档的末尾,如果可能的话具体重复 1,000 次。

我希望这不会太过分 "broad" 因为我有代码并且正在问一个具体问题。

您可以通过将其包装到 For...Next 循环中来重复您的指令集,这在 VBA 应用程序中是相当标准的。这段代码创建了一个循环和一个 'index' 变量,然后告诉它精确地遍历该索引 1000 次。

For index As Integer = 1 To 1000
    Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
    Selection.InsertRowsBelow 1
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
    Selection.Cells.Merge
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.Cut
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.Font.Size = 4
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Cut
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.Font.Size = 3
    Selection.TypeText Text:="Unique specimen identifier not a property tag"
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
Next

如果您需要循环无限次数的内容,例如当满足特定条件时,您可以使用 Do...WhileDo...Until 循环。