1

I try to copy the strContents into clipboard but I can't. My code is:

Dim value_currency As Variant
Dim strContents As String

value_currency = value_currency_txt.value
strContents = NumberToString(value_currency) & " (" & Format(value_currency_txt.Value, "Currency") & ")"

DoCmd.RunCommand acCmdCopy

NumberToString converts number to string.

When I use MsgBox strContents gives me the desired result but when I try to copy the strContents it gives me an error 2406 - the command copy isn't available now.

How can I copy the text into clipboard?

George Kis
  • 155
  • 2
  • 9

2 Answers2

1

If the following is going to work, then this question is a duplicate:

Dim value_currency As Variant
Dim strContents As String

strContents = NumberToString(value_currency) & " (" & Format(value_currency_txt.Value, "Currency") & ")"

Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText strContents
clipboard.PutInClipboard
Community
  • 1
  • 1
Lipis
  • 21,388
  • 20
  • 94
  • 121
1

If you have a form at hand, you can use this simple method:

You can easily send a command that copies the currently selected text in a textbox of the form to the clipboard:

   DoCmd.RunCommand acCmdCopy

Create a button and a (tiny) textbox on the main form: btnClip and txtClip.

Assign this code to the button:

 Private Sub btnClip_Click()
     Dim strClip As String

     ' Empty textbox to copy from.
     Me.txtClip.Value = Null
     ' Retrieve your data.
     strClip = "Some text to copy for later pasting."
     If Len(strClip) > 0 Then
         ' Insert the text in the textbox where it can be copied.
         Me.txtClip.Value = strClip
         ' Move focus to the textbox which will select all text.
         Me.txtClip.SetFocus
         ' Copy the selected text to the clipboard.
         DoCmd.RunCommand acCmdCopy
         ' Return focus to the button.
         Me.btnClip.SetFocus
     End If
 End Sub

The trick is the small textbox, txtClip. It can be placed anywhere and doesn't even need to have a size; it can be zero x zero.

Gustav
  • 53,498
  • 7
  • 29
  • 55