找到你要的答案

Q:Paste text to textbox via button VBA Excel

Q:粘贴文本到文本框通过VBA Excel按钮

I want a copy and a paste button in MS Excel, the copy button looks like this

Private Sub CommandButton1_Click()
Dim MyData As New DataObject
MyData.SetText TextBox1.Text
MyData.PutInClipboard
End Sub

Now, how can I make/code a PASTE button in a similar fashion?

我想在Excel中复制和粘贴按钮,复制按钮看起来像这样

Private Sub CommandButton1_Click()
Dim MyData As New DataObject
MyData.SetText TextBox1.Text
MyData.PutInClipboard
End Sub

现在,我怎样用类似的方式制作/粘贴一个粘贴按钮呢?

answer1: 回答1:

As mentioned in the comments, the post from Get text from clipboard using GetText - avoid error on empty clipboard helped me arrive at the solution that I was looking for.

    Dim DataObj As MsForms.DataObject
    Set DataObj = New MsForms.DataObject 

    On Error GoTo Whoa

    '~~> Get data from the clipboard.
    DataObj.GetFromClipboard

    '~~> Get clipboard contents
    Me.txtKordinatat.Value = DataObj.GetText(1)

    Exit Sub
Whoa:
    If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty"

在评论中提到,后从文本使用gettext -剪贴板避免清空剪贴板错误帮助我到达的解决方案,我在寻找。

    Dim DataObj As MsForms.DataObject
    Set DataObj = New MsForms.DataObject 

    On Error GoTo Whoa

    '~~> Get data from the clipboard.
    DataObj.GetFromClipboard

    '~~> Get clipboard contents
    Me.txtKordinatat.Value = DataObj.GetText(1)

    Exit Sub
Whoa:
    If Err <> 0 Then MsgBox "Data on clipboard is not text or is empty"
excel  vba  excel-vba  userform