Access The Nth Item of Clipboard

孤街浪徒 提交于 2020-08-27 14:26:19

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/37463817/access-the-nth-item-of-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!