Consulting

Results 1 to 4 of 4

Thread: Copying Specific Clipboard Items

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Copying Specific Clipboard Items

    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

  2. #2
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Thanks for that gmayor.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •