Excel - copy string to clipboard

Posted on 2014-03-30
Last Modified: 2014-03-30
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.
Question by:shaunwingin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 27

Assisted Solution

MacroShadow earned 334 total points
ID: 39964832
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 
End Sub 

Open in new window


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 
    Selection = oDataObject.GetText 
End Sub 

Open in new window

LVL 24

Expert Comment

ID: 39964836
Try the following

Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText "PASSWORD"

Author Comment

ID: 39964870
I don't see the reference listed for:
'Microsoft Forms 2.0 Object Library"
Using Excel 2013
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39964881
say I found it by searching for file mf20.dll
However the code doesn't work...
LVL 24

Assisted Solution

mankowitz earned 166 total points
ID: 39964931
The following worked for me

Private Sub button1_click()
With New MSForms.DataObject
    .SetText "password"
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
LVL 27

Accepted Solution

MacroShadow earned 334 total points
ID: 39964989
Not sure why it's not working for you, works fine for me.

Perhaps Chip Pearson's article will help

Author Comment

ID: 39965015
seems I needed to change to clipboard1 as clipboard was reserved??
I also dimmed the text as a string but doubt this is needed.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question