【问题标题】:Copy text using VBA in Access 2007 [duplicate]在 Access 2007 中使用 VBA 复制文本 [重复]
【发布时间】:2015-10-09 09:01:50
【问题描述】:

我尝试将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 - 命令副本现在不可用。

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 它给了我错误用户定义的类型未定义。我认为MSForms.Dataobjects 有问题
    • 我必须从 windows system32 添加 FM20.dll 作为参考
    【解决方案2】:

    如果你手头有表格,你可以使用这个简单的方法:

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

       DoCmd.RunCommand acCmdCopy
    

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

    将此代码分配给按钮:

     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 零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多