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

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

问题描述

使用VBA ACCESS2003 / 2007内。

Using VBA inside Access2003/2007.

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

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

本网站建议创建一个零长度文本框,该字符串复制到文本框,然后运行 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知识库文章,向我们展示了如何做到这一点,但它涉及到许多Windows API调用的。育。

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

是那些只有两个选择?

推荐答案

VB 6提供了一个剪贴板对象,使所有这一切非常简单和方便,但不幸的是,这不是从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.

然而,一个更简单的方法是使用数据对象类,这是窗体库的一部分。我只想建议,如果你已经在使用你的应用程序窗体库功能走这条路。添加一个引用到该库的只有的使用剪贴板似乎有点傻。

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.

例如,把一些文字到剪贴板上,您可以使用下面的code:

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天全站免登陆