在Access 2007中使用VBA复制文本

时间:2015-07-20 09:35:07

标签: vba ms-access ms-access-2007

我尝试将strContents复制到剪贴板中,但我不能。 我的代码是:

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将数字转换为字符串。

当我使用MsgBox strContents时,我会得到所需的结果,但是当我尝试复制strContents时,它会给我一个错误2406 - 命令副本现在不可用。

如何将文本复制到剪贴板?

2 个答案:

答案 0 :(得分:1)

如果以下内容有效,则此问题为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

答案 1 :(得分:1)

如果您手头有表格,可以使用以下简单方法:

您可以轻松地将表单文本框中当前所选文本的命令发送到剪贴板:

   DoCmd.RunCommand acCmdCopy

在主窗体上创建一个按钮和一个(小)文本框: btnClip txtClip

将此代码分配给按钮:

 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

技巧是小文本框,txtClip。它可以放在任何地方,甚至不需要有尺寸;它可以是零x零。