Is it possible to check content of clipboard before pasting it in Excel VBA
I have this today:
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 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
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
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).
Thanks a lot Guys..! You are amazing! I just wanted to share my solution with you.
Function GetClipboardText(nChars As Integer) As String
Dim BufObj As MSForms.DataObject
Set BufObj = New MSForms.DataObject
BufObj.GetFromClipboard
GetClipboardText = Left(BufObj.GetText, nChars) ' Get only first nChars
End Function
Sub CreateOverviewSheet()
' Determine type of report in Clipboard
IsMultiLevel = (InStr(GetClipboardText(100), "Multi-Level") > 0)
IsConsolidated = (InStr(GetClipboardText(100), "Consolidated") > 0)
If Not IsMultiLevel Or IsConsolidated Then
MsgBox ("ERROR in Clipboard Data!!")
End
End If
Sheets.Add After:=Sheets(Sheets.Count) ' Create new sheet
ActiveSheet.Paste ' Paste from Clipboard
.
. and so on...
.
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With