Copy textbox text to clipboard (1 Viewer)

stu_c

Registered User.
Local time
Today, 20:34
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.
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!
 
Now that we are on a collection of clipboards, here's my version (without the need of any reference).
You can read/write to clipboard.

SQL:
Function Clipboard(Optional StoreText As String) As String
  
    Dim txt As Variant
 
    ' Store as variant for 64-bit VBA support
    txt = StoreText

    With CreateObject("htmlfile")
        With .parentWindow.clipboardData
            Select Case True
                Case Len(StoreText)
                    .setData "text", txt
                Case Else
                    Clipboard = .GetData("text")
            End Select
        End With
    End With

End Function

Usage:
SQL:
clipbard "Copy this text"

&

MyVar = clipboard
 
Last edited:
OK, now I see what was going on with the 2046 error. The text to be copied must be highlighted or selected before that command will be effective. So an even better way if using the double click event is to simply set focus elsewhere first then back to the target textbox. This ensures the entire text gets selected.

Code:
Private Sub Notes_DblClick(Cancel As Integer)
   Me.Phone.SetFocus
   Me.Notes.SetFocus
   DoCmd.RunCommand acCmdCopy
End Sub

Here is another Article from Philips site on a pre-declared class from 2019 that retrieves the contents of the clipboard programmatically and assigns to a variable for use in your program to do whatever preprocessing of the data that you want. I had to convert the class for use with 64bit, so that is attached below if anyone is interested. I tried to do a test with it, but keep getting a failed to lock clipboard memory error. So maybe @sonic8 can comment on this.

1741962745009.png
 

Attachments

The text to be copied must be highlighted or selected before that command will be effective.
That's correct! Something must be selected before it can be copied (Ctrl+C) and pasted (Ctrl+V).
 

Users who are viewing this thread

Back
Top Bottom