• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3042
  • Last Modified:

Excel - copy string to clipboard

Say, I don't want to copy from a cel - but rather the text in a string - its a password that needs to be put in the clipboard. I'm using a macro/VBA code.
0
shaunwingin
Asked:
shaunwingin
  • 3
  • 2
  • 2
3 Solutions
 
MacroShadowCommented:
1. Add a reference to the 'Microsoft Forms 2.0 Object Library'
2. This will put text in the clipboard:
Sub CopyToClipboard(strInput As String) 
    Dim oDataObject As New DataObject 
    oDataObject.SetText strInput 
    oDataObject.PutInClipboard 
End Sub 

Open in new window

Now
CopyToClipboard("Password")

Open in new window

Will put Password in the Windows clipboard
3. This will paste from Windows clipboard:
Sub PasteFromClipboard() 
    Dim oDataObject As New DataObject 
    oDataObject.GetFromClipboard 
    Selection = oDataObject.GetText 
End Sub 

Open in new window

0
 
mankowitzCommented:
Try the following

Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText "PASSWORD"
clipboard.PutInClipboard
0
 
shaunwinginAuthor Commented:
I don't see the reference listed for:
'Microsoft Forms 2.0 Object Library"
Using Excel 2013
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
shaunwinginAuthor Commented:
say I found it by searching for file mf20.dll
However the code doesn't work...
mankowitz
0
 
mankowitzCommented:
The following worked for me

Private Sub button1_click()
With New MSForms.DataObject
    .SetText "password"
    .PutInClipboard
End With
End Sub

you have to make sure that msforms object is loaded. You can either add the reference as you did, or just add a userforms object to the page. You can always delete it. For example, from the Developer tab, click
Insert > more > select something like WINDOWS FORMS 2.0 TEXTBOX
0
 
MacroShadowCommented:
Not sure why it's not working for you, works fine for me.

Perhaps Chip Pearson's article will help http://www.cpearson.com/excel/Clipboard.aspx
0
 
shaunwinginAuthor Commented:
seems I needed to change to clipboard1 as clipboard was reserved??
I also dimmed the text as a string but doubt this is needed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now