宏重复粘贴12次

时间:2017-03-24 18:19:21

标签: vba loops ms-word

我想创建一个宏来重复相同的段落12次,然后移到下一个,重复上面的功能直到文档结束。

我试过,现在我想为下面的宏创建一个循环,直到文档的结尾,所以任何帮助都非常感谢:

Sub SelectRange()
Selection.Paragraphs(1).Range.Copy
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Paste
Selection.Next(Unit:=wdParagraph, Count:=1).Select

End Sub

2 个答案:

答案 0 :(得分:1)

对于这个特定的问题,我会使用@Corith_Malin的答案,但稍作调整。您必须为文档中的所有当前段落创建一个容器(在这种情况下,我将使用我使用后期绑定创建的ArrayList)。在容器上有所有段落后,您可以循环遍历它们并使用之前提供的循环粘贴它们,如下所示:

Sub SelectRange()

'Declaration and assignation of the ArrayList
Dim arrList as Object
Set arrList = CreateObject("System.Collections.ArrayList")

'Loop through each paragraph and store it on the ArrayList
For Each par In ActiveDocument.Paragraphs
    arrList.Add (par)
Next

'Loop through each stored paragraph
For Each Item in arrList
    Item.Range.Copy

    'Loop 12 times and perform a paste operation
    For i = 1 To 12
        Selection.Paste
    Next

Next

End Sub

请测试一下,让我知道你的意见。它适用于我的电脑(程序员笑话;))

答案 1 :(得分:0)

如果你只是好奇如何使用循环代替Selection.Paste 12次......

Sub SelectRange()
Selection.Paragraphs(1).Range.Copy

' Loop 12 times and perform a paste operation
For i = 1 To 12
  Selection.Paste
Next i

' You may need to set Count:=12 here to skip over the pasted content.
Selection.Next(Unit:=wdParagraph, Count:=1).Select

End Sub