Adding a multiple cut/paste feature to MS Word

<<Return to start of this example.

Part 2: Adding VBA code to the Multi-Paste Dialog

Let's deal with the Close button first, it is the the simplest operation but requires us to think about how we handle the form.

The idea is that the form will be displayed when a user clicks on a ToolBar button. The form will then appear, displaying the text that is stored in the ClipManagerX control. The ClipManagerX control will need to be activated when we first start-up Word (we will come to that later). It is clear that the ClipmanagerX control must remain in memory at all times, otherwise any data it has collected will be lost, and therefore, the form which owns it must also remain in memory at all times.

The conclusion is that we must Load the form when we start-up Word and only Unload it when Word is closed. In between we will only Show or Hide the form, but it will always be there. In that case, the function of the Close button is simple, it just has to Hide the form.

Add the following lines to the code window of the UserForm:

Private Sub cmdClose_Click()
  Me.Hide
End Sub

Next, the Clear button. The purpose of this is to clear all the items currently held in the ClipManagerX control. At the same time we need to clear the ListBox. The code to be added is self-explanatory:

Private Sub cmdClear_Click()
  ClipMan1.Clear
  ListBox1.Clear
End Sub

Finally, the Paste button. This is slightly more complex as we must first determine whether or not any text is currently selected in Word. If text is selected, this should be deleted before being replaed by the pasted text, but if none is selected, the delete is unnecessary. We can test to see if text is selected by checking whether or not the Type property of the current Selection is wdSelectionIP (An Insertion Point).

After that, we read and insert the text from ClipManagerX according to the item selected in the list box. As you will see on the next page, each text clip will have been collected in two formats if available - Plain Text and Rich Text. While only the plain text is displayed in the ListBox, we will choose to paste into Word in Rich Text Format (RTF) if it is available.

Finally the screen is refreshed to show the changes.

Private Sub cmdPaste_Click()
  If Selection.Type <> wdSelectionIP Then
    Selection.Delete
  End If
  If ClipMan1.HasFormat(ListBox1.ListIndex + 1, RTFCode) Then
    ClipMan1.CopyToClipboard ListBox1.ListIndex + 1, RTFCode
  Else
    ClipMan1.CopyToClipboard ListBox1.ListIndex + 1, 1
  End If
  Selection.Paste
  Application.ScreenRefresh
End Sub

The last steps are to activate the control on start-up and provide the ToolBar button to bring up the new Dialog... Next page