简体   繁体   English

如何将文本从 vba 复制到 windows 剪贴板

[英]how copy text from vba to windows clipbaord

I have an ms access vba code from which i wish to copy some text value to the Windows clipboard so that I can paste it elsewhere (Word/Excel/Notepad/etc).我有一个 ms 访问 vba 代码,我希望从中复制一些文本值到 Windows 剪贴板,以便我可以将其粘贴到其他地方(Word/Excel/记事本/等)。

I have been searching for this in SO but everything seems over-complicated.我一直在寻找这个,但一切似乎都过于复杂。 Should it not be something simple like它不应该是简单的事情吗

clipboard.SetText textValue剪贴板.SetText textValue

? ?

EDIT编辑

I tried following the hint by BrianMStafford but don't succeed.我尝试按照 BrianMStafford 的提示进行操作,但没有成功。 Perhaps the reason is that my object is a node in a tree.也许原因是我的对象是树中的一个节点。 When I do当我做

MsgBox Me.NodeKey.Value MsgBox Me.NodeKey.Value

it all works fine - I see the node path in the message box.一切正常 - 我在消息框中看到节点路径。

在此处输入图像描述

But when I do但是当我这样做时

Me.NodeKey.SetFocus Me.NodeKey.SetFocus
DoCmd.RunCommand acCmdCopy DoCmd.RunCommand acCmdCopy

I don't get the node path in the clipboard我没有在剪贴板中获得节点路径

So how can I copy the node path value into the Windows clipboard?那么如何将节点路径值复制到 Windows 剪贴板中呢?

Public Sub WriteToClipboard(ByVal text As String)
    CreateObject("htmlfile").ParentWindow.ClipboardData.SetData "text", text
End Sub

Example usage:示例用法:

WriteToClipboard "test"

Edit #1编辑#1

The above seems to work in Excel just fine.以上似乎在Excel中工作得很好。 I only managed to make it work in Access for a specific computer.我只设法让它在特定计算机的 Access 中工作。 Once tested on another computer I got an error 70 (access denied).在另一台计算机上测试后,我收到错误 70(拒绝访问)。

The below only works in Excel if Windows Explorer is closed.如果关闭 Windows 资源管理器,以下内容仅适用于 Excel。 However, it seems to work fine in Access regardless if Explorer is open/closed:但是,无论资源管理器是否打开/关闭,它似乎都可以在 Access 中正常工作:

Public Sub WriteToClipboard(ByVal text As String)
    With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 'MsForms.DataObject
        .SetText text
        .PutInClipboard
    End With
End Sub

It should be as simple as you say, however Access for some reason doesn't have the same "options" as excel.它应该像您说的那样简单,但是由于某种原因,Access 没有与 excel 相同的“选项”。 You have to add it manually or use Cristian Buse's answer which is pretty much the same, he is skipping adding the reference.您必须手动添加它或使用几乎相同的 Cristian Buse 的答案,他正在跳过添加参考。 So here is the way to manually add the reference to use .SetText in Access.所以这里是手动添加引用以在 Access 中使用.SetText的方法。

Access does not have the MS Forms listed on the Reference table like Excel (dont ask me why), but you can Browse to the file location and add it manually: Access 没有像 Excel 这样的参考表中列出的 MS 表单(不要问我为什么),但您可以浏览到文件位置并手动添加它:

在此处输入图像描述

After you add this manually you can use the .SetText anywhere as long as you also declare the object of course.手动添加后,您可以在任何地方使用 .SetText,只要您当然还声明了对象。

Sub testCopy()
    Dim clipOb As MSForms.DataObject
    Set clipOb = New MSForms.DataObject
    clipOb.SetText Format(Now(), "m/d/yyyy")
    clipOb.PutInClipboard
End Sub

Now you can paste it anywhere.现在您可以将其粘贴到任何地方。 Just replace Format(Now(), "m/d/yyyy") with your Me.NodeKey.Value .只需将Format(Now(), "m/d/yyyy")替换为您的Me.NodeKey.Value If your Value is empty/blank it will give you an error, so just do a check before using .SetText to make sure you have a value to set.如果您的 Value 为空/空白,它将给您一个错误,因此只需在使用.SetText之前进行检查,以确保您有一个要设置的值。

Use Windows API to Set Clipboard Data.使用 Windows API 设置剪贴板数据。

Here's the documentation page: https://docs.microsoft.com/en-us/office/vba/access/concepts/windows-api/send-information-to-the-clipboard这是文档页面: https ://docs.microsoft.com/en-us/office/vba/access/concepts/windows-api/send-information-to-the-clipboard

Here's the code required with 2 example of how to use.这是 2 个如何使用示例所需的代码。 After running sub to save data to clipboard, just paste it anywhere.运行 sub 将数据保存到剪贴板后,只需将其粘贴到任何地方。

Option Explicit

Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare Function CloseClipboard Lib "user32.dll" () As Long
Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As Long, ByVal lpString2 As Long) As Long

Public Sub SetClipboard(sUniText As String)

    Dim iStrPtr As Long
    Dim iLen As Long
    Dim iLock As Long
    
    Const GMEM_MOVEABLE As Long = &H2
    Const GMEM_ZEROINIT As Long = &H40
    Const CF_UNICODETEXT As Long = &HD
    
    OpenClipboard 0&
    EmptyClipboard
    
    iLen = LenB(sUniText) + 2&
    iStrPtr = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, iLen)
    iLock = GlobalLock(iStrPtr)
    lstrcpy iLock, StrPtr(sUniText)
    GlobalUnlock iStrPtr
    SetClipboardData CF_UNICODETEXT, iStrPtr
    CloseClipboard
    
End Sub

Sub Example_SetClipboardData_1()

    SetClipboard "Hello World"
    
End Sub

Sub Example_SetClipboardData_2()
    
    Dim TextVar As String
    
    TextVar = "Hello again, World"
    SetClipboard TextVar
    
End Sub

When googling the object code that Cristian posted, I found a good page listing the possibilities, both early and late binding.在谷歌搜索 Cristian 发布的目标代码时,我发现一个很好的页面列出了早期绑定和后期绑定的可能性。 Hence this page combines the answers by Christian Buse and Ricardo A.因此,此页面结合了 Christian Buse 和 Ricardo A 的答案。

See

https://desmondoshiwambo.wordpress.com/2012/02/23/how-to-copy-and-paste-text-tofrom-clipboard-using-vba-microsoft-access/ https://desmondoshiwambo.wordpress.com/2012/02/23/how-to-copy-and-paste-text-tofrom-clipboard-using-vba-microsoft-access/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM