A VBScript function to get text data from the Windows clipboard26 Jul 2010
Earlier today I posted about some VBScript that deals with variable positional formatting of tabs within strings. This was related to a system I was working with where the information needed to be copied from the screen. In an attempt to make things simpler for the user my overall script worked with the Windows clipboard as the data source rather than having the user paste it into a text file and then import that into the script. In case you haven’t come across this technique before, here’s how I did it.
There is no VBScript to directly access the Windows clipboard, so we need to control an application to do it. Internet Explorer has been a good standard choice and whilst there have been options put in place by Microsoft this year to allow people to select not to install Internet Explorer, it remains a good option for the majority of PCs and probably any that you might be choosing to use VBScript on (rather than say PowerShell).
So we start by creating an instance of the Internet Explorer application and then set the page to a blank one rather than any default home page. This ensures we have a nice speedy response and don’t necessarily need an Internet connection. We then take the content of the clipboard and pass it back to the return variable for the function. Please note this is only for text data. Anything beyond simple text has a level of complexity to it that I would highly recommend defaulting out to a good solid programming platform rather than a scripting platform. Finally the script closes Internet Explorer and the clipboard content will be passed back to the calling line of script.
Function FetchClipboardData() Dim objMSIE Set objMSIE = CreateObject("InternetExplorer.Application") objMSIE.Navigate("about:blank") FetchClipboardData = objMSIE.document.parentwindow.clipboardData.GetData("text") objMSIE.Quit End Function