Adding a multiple cut/paste feature to MS Word

<<Return to start of this example.

Part 3: Activation of the Control at Start-up and Opening the New Dialog

Next, add a new code module to the VBA project using Insert/Module from the menu. We have left the name of this module as Module 1, the default.

When Word starts up, it will attempt to run any procedure with the name "AutoExec", so we have added a procedure with this name to the new module. This is where we will put the code to activate the ClipManagerX control.

This procedure does several things. First it loads (and hides) the Form containing the ActiveX control. As discussed earlier, the form must remain loaded at all times so that the control remains active. Next the control is activated, then various properties of the control are set in order to instruct it to collect clipboard data in two formats - Plain Text and Rich Text (RTF). RTF is a custom format and so the integer value used by Windows to identify it needs to be captured. This is done by setting the variable RTFCode to the return value of the AddFormat method. Finally, the MaxItems property is set to collect up to 10 clips of text.

The procedure is shown below:

Public Sub AutoExec()
  Load frmClip
  With frmClip.ClipMan1
    .DelayMode = 10
    .AllFormats = False
    .AddFormat (cfText)
     RTFCode = .AddFormat(cfRichText)
    .MaxItems = 10
  End With
End Sub

The code to be executed in order to bring up the multi-paste dialog will also go in this module. This clears the contents of the ListBox, populates the ListBox with the text entries currently held in ClipManagerX, and then shows the form (which will later be hidden by the Close button):

Public Sub ShowClips()
  Dim i As Integer
  With frmClip
    For i = 1 To .ClipMan1.MaxItems
      .ListBox1.AddItem (.ClipMan1.Text(i))
    Next i
    End With
End Sub

The final step to put a finishing touch on the application is to add a new Toolbar in Word with a button that calls the "ShowClips" procedure. This is done using View/Toolbars/Customize from the Word menu and adding a new Toolbar. By going to the Macros category on the Commands tab a list of the VBA procedures in Module1 will be displayed. ShowClips can be selected and dragged onto the new toolbar.

In our example template, we have also customised the ToolBar button and added a copy button to the same ToolBar for completeness.