问题描述
是否可以在将剪贴板的内容粘贴到Excel VBA中之前检查剪贴板的内容
Is it possible to check content of clipboard before pasting it in Excel VBA
我今天有这个东西:
Sheets.Add After:=Sheets(Sheets.Count) ' Create new sheet
ActiveSheet.Paste ' Paste from Clipboard
IsMultiLevel = (InStr(Range("A1"), "Multi-Level") > 0) ' Determine type of report
If Not IsMultiLevel Then
MsgBox ("ERROR in Clipboard Data!!")
End
Else
ActiveSheet.Delete
End If
Bu我希望检查数据在添加新表之前,然后我不需要删除它。.我想要这样的东西
Bu I whish to check data before adding new sheet, then I dont need to delete it.. I want somthing like this
IsMultiLevel = (InStr([CLIPBOARD], "Multi-Level") > 0) ' Determine type of report
If Not IsMultiLevel Then
MsgBox ("ERROR in Clipboard Data!!")
End
End If
Sheets.Add After:=Sheets(Sheets.Count) ' Create new sheet
ActiveSheet.Paste ' Paste from Clipboard
推荐答案
来自Excel 2003我必须说可以通过使用剪贴板来检查剪贴板内容 MSForms.DataObject
。首先必须创建对Microsoft Forms 2.0 Object库的引用(VBA窗口工具/引用)(通常在... \system32\FM20.DLL中找到)。
Coming from Excel 2003 I must say it is possible to examine the clipboard content by making use of the MSForms.DataObject
. You first have to create a reference (VBA window tools / reference) to the Microsoft Forms 2.0 Object library (usually found at ...\system32\FM20.DLL).
然后,您可以将剪贴板读入文本变量:
Then you can read the clipboard into a text variable:
Dim BufObj As MSForms.DataObject, BufTxt as String
Set BufObj = New MSForms.DataObject
BufObj.GetFromClipboard
BufTxt = Buf.GetText
缓冲区文本将保持不变(至少在Win XP / SP3,MS Office 2003 SP 3中可用),并可供进一步使用,即 GetFromClipboard
不会破坏剪贴板缓冲区。这里要考虑的是剪贴板内容可以作为文本使用,因此任何图形都将以原始文本模式存储。另外,还需要考虑缓冲区大小,因为Excel中的可变长度字符串最多可以容纳ca。 2 ^ 31个字符(恕我直言,这应该足以满足所有需求的90%)。
The buffer text will remain untouched (at least in Win XP/SP3, MS Office 2003 SP 3) and available for further use, i.e. the GetFromClipboard
won't destroy the clipboard buffer. The thing to consider here is that the clipboard content is available "as text" so any graphic will be stored in a raw text mode. Also the buffer size needs to be considered, as a variable length string in Excel can hold not more than ca. 2^31 characters (but IMHO this should be enough for 90% of all needs).
这篇关于粘贴前检查剪贴板的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!