PDA

View Full Version : Copying Specific Clipboard Items



Jfp87
09-16-2016, 12:30 AM
Guys,

I am using an inkEdit control within a userform to preview blocks of formatted text which are stored in a separate Word document.

The userform has an Edit button which gives the user the option to edit the text displayed in the inkEdit control. When the user clicks 'Edit', the content from the separate Word document is copied to the clipboard and pasted into the current document so that it can be edited. This means that the last item to be copied by the user is not the first item which will be pasted in a subsequent paste action (due to the content being copied to the clipboard in the background).

My question is, how can I accomplish this task without disrupting the user's copy/paste action? I am aware that the user could open the clipboard and select the correct item to paste, but I don't want that. The last item copied to the clipboard by the user should be the first item pasted.

I seen some functions which assign keys to clipboard items - is this the way forward, or is there a simpler approach?

Joe

gmayor
09-16-2016, 05:22 AM
Instead of using COPY and PASTE to transfer the data between the two documents, use ranges. Set a range to the item you want to 'copy' (Range1) and another to the location where you want to insert it (Range2) and use
Range2.formatted text = Range1.Formattedtext. The clipboard is not involved.

Jfp87
09-17-2016, 08:22 AM
Thanks for that gmayor.

gmaxey
09-17-2016, 04:25 PM
You can also store formatted text in a collection:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCol As New Collection
Dim oDoc As Document
Dim oRng As Range
oCol.Add ActiveDocument.Paragraphs(1).Range.FormattedText
oCol.Add ActiveDocument.Paragraphs(2).Range.FormattedText
Set oDoc = Documents.Add
Set oRng = oDoc.Range
oRng.Text = vbCr + vbCr
'Traspose the formatted text in the new documetn
oRng.Paragraphs(1).Range.FormattedText = oCol.Item(2)
oRng.Paragraphs(2).Range.FormattedText = oCol.Item(1)
lbl_Exit:
Exit Sub
End Sub