访问剪贴板的第N个项目 [英] Access The Nth Item of Clipboard

查看:132
本文介绍了访问剪贴板的第N个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从剪贴板中检索多个项目?我正在使用这样的东西:

 将剪贴板作为MSForms.DataObject 
将Dim str1作为字符串
Dim str2 As String

设置剪贴板=新的MSForms.DataObject

剪贴板.GetFromClipboard
str1 =剪贴板.GetText(1)
str2 =剪贴板。 GetText(2)

但是,在为第二个变量赋值时出现错误以下内容:

 运行时错误'-2147221404(800040064)':
DataObject:GetText无效的FORMATETC结构

非常感谢您的帮助!

解决方案

结果有两个剪贴板:Windows剪贴板和Office剪贴板。



Office剪贴板最多可容纳24个项目(所有是相同的类型),而Windows剪贴板只能容纳每种类型的一项。




  • 复制到Windows剪贴板就像突出显示然后
    键入Ctrl-C一样容易。

  • 复制到Office剪贴板就像突出显示
    一样简单,然后键入Ctrl-CC。只有在
    时至少有一个打开并处于活动状态的Office应用程序,Office剪贴板才处于活动状态



<在VBA中,使用MSForms.DataObject仅使您能够访问Windows剪贴板,因此只有一个文本项可用。经过各种搜索后,我无法找到如何使用VBA中的Office剪贴板打开/控制/复制/粘贴的内容。



说了这么多,在编写和运行VBA宏时,几乎没有理由使用任何剪贴板。您可以(临时)将这些值存储在工作表上未使用的单元格中,公共变量,公共对象甚至私有变量中(可能具有可公开访问的属性)。所有这些方法的目的与将数据存储在剪贴板中的目的完全相同。



在围绕该主题的interwebz进行浏览时,我遇到了一些使用剪贴板来在不同的工作簿之间或在Excel-to-Word之类的Office应用之间复制数据。您仍然不需要剪贴板,因为VBA可以打开远程/外部应用程序/工作簿/文档并直接复制/粘贴数据。


Is there a way to retrieve several items from the clipboard? I'm using something like this:

Dim clipboard As MSForms.DataObject
Dim str1 As String
Dim str2 As String

Set clipboard = New MSForms.DataObject

clipboard.GetFromClipboard
str1 = clipboard.GetText(1)
str2 = clipboard.GetText(2)

However, I get an error where I assign a value to my second variable that says the following:

  Run-time error '-2147221404 (800040064)':
  DataObject:GetText Invalid FORMATETC Structure

Help is much appreciated!

解决方案

Turns out there are two clipboards: the Windows clipboard and the Office clipboard.

The Office clipboard can hold up to 24 items (all can be the same type), whereas the Windows clipboard can only hold one item of each type.

  • Copying to the Windows clipboard is as easy as highlighting then typing Ctrl-C.
  • Copying to the Office clipboard is as easy as highlighting then typing Ctrl-CC. The Office clipboard is only active if there is at least one Office application open and active at the time.

In VBA, using the MSForms.DataObject only gets you access to the Windows clipboard, so there is only a single text item available. After a variety of searches, I'm not able to find out how to open/control/copy/paste using the Office clipboard from VBA.

Having said all that, there is likely little reason to use any clipboard when writing and running a VBA macro. You can (temporarily) store those values in unused cells on a worksheet, in a public variable, in a public object, or even a private variable (probably with publically accessible properties). All of those methods serve exactly the same purpose as storing data in the clipboard.

In skimming around the interwebz on this topic, I ran across several references to using the clipboard to copy data between different workbooks, or between Office apps like Excel-to-Word. You still don't need the clipboard for this, as VBA can open the remote/external application/workbook/document and copy/paste the data directly.

这篇关于访问剪贴板的第N个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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