如何使用 Access/VBA 复制到剪贴板? [英] How to copy to clipboard using Access/VBA?

查看:81
本文介绍了如何使用 Access/VBA 复制到剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Access2003/2007 中使用 VBA.

Using VBA inside Access2003/2007.

如何将字符串变量的内容复制到剪贴板?

How to copy the contents of a string variable to the clipboard?

本站推荐创建一个零长度的TextBox,复制字符串到文本框,然后运行 ​​DoCmd.RunCommand acCmdCopy.啊.我的意思是,我们可能会沿着这条路线走下去.但是还是.呃.

This site recommends a creating a zero length TextBox, copying the string to the TextBox, then running DoCmd.RunCommand acCmdCopy. Ugh. I mean, we may go down the route. But still. Ugh.

虽然 MS 知识库文章向我们展示了如何做到这一点,但它涉及许多 WindowsAPI 调用.嗯.

While the MS knowledgebase article shows us how to do it but it involves a number of Windows API calls. Yuk.

只有这两种选择吗?

推荐答案

VB 6 提供了一个 Clipboard 对象,使这一切变得极其简单和方便,但不幸的是,VBA 没有提供.

VB 6 provides a Clipboard object that makes all of this extremely simple and convenient, but unfortunately that's not available from VBA.

如果是我,我会走 API 路线.没有理由害怕调用原生 API;该语言为您提供这样做的能力是有原因的.

If it were me, I'd go the API route. There's no reason to be scared of calling native APIs; the language provides you with the ability to do that for a reason.

然而,更简单的替代方法是使用 DataObject 类,它是 Forms 库的一部分.如果您已经在应用程序中使用 Forms 库中的功能,我只会建议您走这条路.添加对该库的引用以使用剪贴板似乎有点愚蠢.

However, a simpler alternative is to use the DataObject class, which is part of the Forms library. I would only recommend going this route if you are already using functionality from the Forms library in your app. Adding a reference to this library only to use the clipboard seems a bit silly.

例如,要在剪贴板上放置一些文本,您可以使用以下代码:

For example, to place some text on the clipboard, you could use the following code:

Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText "A string value"
clipboard.PutInClipboard

或者,将剪贴板中的文本复制到字符串变量中:

Or, to copy text from the clipboard into a string variable:

Dim clipboard As MSForms.DataObject
Dim strContents As String

Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
strContents = clipboard.GetText

这篇关于如何使用 Access/VBA 复制到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆