如何循环现有Word宏?需实现1000次重复执行需求
How to Repeat a Word VBA Macro for All Rows in a 2-Column Table
Got it, let's tackle this. You've got a solid macro sequence that positions the cursor perfectly for repeated runs—nice work. To make it run automatically either exactly 1000 times or until the end of your table, here are two straightforward solutions:
Option 1: Repeat Exactly 1000 Times
Wrap your existing code in a For...Next loop. This is the simplest approach if you know you need exactly 1000 repetitions. Just drop your current code inside the loop block:
Sub RepeatMacro1000Times() Dim i As Integer ' Loop through 1000 iterations For i = 1 To 1000 ' Your existing macro code starts here 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 ' Your existing macro code ends here Next i End Sub
Option 2: Repeat Until the End of the Table
If you want the macro to stop automatically when it reaches the last row of your table (instead of a fixed number of runs), use a Do While loop. This checks if you're still on a row that needs processing:
Sub RepeatUntilTableEnd() ' Store the table object to avoid repeated lookups Dim targetTable As Table Set targetTable = Selection.Tables(1) ' Loop until we reach the last row of the table Do While Selection.Information(wdEndOfRangeRowNumber) < targetTable.Rows.Count ' Your existing macro code starts here 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 ' Your existing macro code ends here Loop End Sub
Quick Notes:
- For the table-end loop: Make sure your cursor starts on the first row you want to process (usually the top row of your table). The loop checks if the current row number is less than the total number of rows in the table—since each run adds a new row, the total count increases, but the loop will stop once you've handled all original rows.
- Using
Selectionworks here because your existing code already positions the cursor correctly for the next run. If you ever run into unexpected cursor jumps, you could refactor to work directly with table rows (e.g.,targetTable.Rows(i)), but this wrap-around approach should work perfectly with your current code.
内容的提问来源于stack exchange,提问作者Steven Messer
相关产品推荐
相关产品推荐

