Jorge Iten Jr
Jorge Iten Jr

Reputation: 362

How to clear Selection in VBA

I am using a simple button to replicate a table in word.
The code:

Private Sub CommandButton1_Click()
 ActiveDocument.Tables(2).Select
 Selection.Copy
 Selection.Paste
End Sub

A very simple code. When I click the button for the first time ok. However, after that whenever I Click the button, it replicates all the tables. I assume it happens because the Selection is not empty. How Can I simply clear the Selection?

Upvotes: 0

Views: 5110

Answers (2)

mr.Reband
mr.Reband

Reputation: 2430

To clear the selection, add this to the end of your sub:

    CutCopyMode = False

Note that that your code doesn't seem to add a copy of the second table, but instead it appends a copy of each row in the second table to the end of that same table. Not sure if that's by design or not.

To copy the entire table to the end of the document:

    ActiveDocument.Tables(2).Select
    Selection.Copy
    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph
    Selection.Paste

Upvotes: 3

Belial09
Belial09

Reputation: 694

does this help?

Private Sub CommandButton1_Click()
    ActiveDocument.Tables(2).Rows(1).Select
    Selection.Copy
    Selection.Paste
End Sub

Upvotes: 0

Related Questions