Copy textbox text to clipboard

stu_c

Registered User.
Local time
Today, 20:02
Joined
Sep 20, 2007
Messages
493
Hi all
I am trying to copy text from my text box into a clipboard but seem to be running into an Active X error any suggestions?

Code:
 Me.TxtReportText.SetFocus
 Dim DataObj As Object
 Set DataObj = CreateObject("MSForms.DataObject")
    DataObj.SetText Me.TxtReportText.Value
    DataObj.PutInClipboard

Thanks in advance
 
add Reference to Microsoft Forms 2.0 Object Library then change your code to:
Code:
Dim clp As MSForms.DataObject
If clp Is Nothing Then
    Set clp = New MSForms.DataObject
End If
With clp

    .SetText  Me.TxtReportText.Value
    .PutInClipboard

End With
 
You might have to Browse for the FM20.dll in order to see that reference @arnelgp pointed out. It wasn't there for me until I browsed for it.
 
For a Textbox, I think I just do something like this:
Code:
Me.TextboxName.SetFocus
DoCmd.RunCommand acCmdCopy
 
For a Textbox, I think I just do something like this:
Code:
Me.TextboxName.SetFocus
DoCmd.RunCommand acCmdCopy
I got the following error using that technique.
1741879312710.png
 
I got the following error using that technique.
View attachment 118933
We must have a different setup. I tested it before I posted it. How did you set it up? I simply added a button to an existing Form and picked one of the Textboxes on it for testing.

PS. Could it be possible that the error message means the Textbox was empty and there was nothing to copy?

Sent from phone...
 
No, I used a double click event on a notes field. There was text in the field and that's the error I got after double clicking. Arnel's method worked perfectly.
 
You can put text on the clipboard with this...
Code:
With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    .SetText < your text goes here >
    .PutInClipboard
End With
This creates a late-bound MSForms.DataObject
 
No, I used a double click event on a notes field. There was text in the field and that's the error I got after double clicking. Arnel's method worked perfectly.
So, as I had suspected, we had a slightly different setup. Also, I didn't say Arnel's suggestion doesn't work. I was just saying I was using a simple approach for a simple task. Since you're using the DoublClk event, there's no need to do the SetFocus. Now, I haven't tried copying a Memo field, if that's what you have (there may be some limitations on the size of the clipboard), but could you please try the following code instead just to see if you'll still get that error message? Thanks.
Code:
Screen.ActiveControl.SelStart = 0
Screen.ActiveControl.SelLength = 1000
DoCmd.RunCommand acCmdCopy
 
Not sure what the difference is, but the new code does work. Simple is always better.
 
Not sure what the difference is, but the new code does work. Simple is always better.
Thanks for giving it a try. I think the difference was the DoublClk event negates the need for SetFocus. However, the error message was confusing (to me, at least). Cheers!
 

Users who are viewing this thread

Back
Top Bottom