So you’re working with WSH and you’re wondering how you can get or set the contents of the Windows clipboard. Unfortunately, WSH does not have this ability natively and there are no COM objects or WMI methods for performing this action either, but you’ll be happy to see this workaround that will let you get and set the clipboard contents using Internet Explorer.
Quite often we find ourselves trying to perform an action or complete a task that is not native to either VBScript or WSH. In these cases, it’s helpful to use another program that has the abilities we’re looking for through its scripting interface. Typically this is done with Internet Explorer.
“Why Internet Explorer?” You ask.
Internet Explorer works very closely with the Windows Shell and therefore has a great deal of functionality that goes beyond simply grabbing and rendering web pages. Furthermore, it also has a very extensive scripting interface that makes the majority of these features available to scripters through its COM interface.
We’re going to harness some of that power now. Because Internet Explorer is designed to be scriptable, we’re able to launch the application and pass client-side browser script to the application as well. Let’s take a look at the code to retrieve the contents of the Windows clipboard.
Set objIE = CreateObject("InternetExplorer.Application") objIE.Navigate("about:blank") strTemp = objIE.document.parentwindow.clipboardData.GetData("text") objIE.Quit WScript.Echo strTemp
In the first two lines, we create an instance of Internet Explorer and navigate it to a blank page. Once we have the application open, we’re able to perform a little workaround.
You’ll notice in the third line, we’re not using the WSH object model. Those objects and methods do not belong to the Internet Explorer object directly. They belong to Internet Explorer’s Document Object Model, but because Internet Explorer is designed to be scriptable, we’re able to pass them through the Internet Explorer object’s COM interface.
We then wrap up our simple example by closing Internet Explorer and displaying the string contents. Simple. However, there are a couple limitations to keep in mind.
First and foremost, the clipboard functionality in Internet Explorer is limited to text data only. You will not be able get or set binary data. So while you couldn’t retrieve an image on the clipboard, you could retrieve a path to one.
Second, this does require an instance of Internet Explorer. This provides a little overhead for your script, but it makes for a native workaround since Internet Explorer is present on nearly every Windows machine.
Now let’s take a look at how to copy information to the clipboard in WSH.
Placing text on the clipboard is just the opposite of taking it off. We’ll just use SetData instead of GetData.
strTemp = "Some clipboard text." & VbCrLf strTemp = strTemp & "It can even contain linefeeds!" Set objIE = WScript.CreateObject("InternetExplorer.Application") objIE.Navigate "about:blank" Do Until objIE.ReadyState = 4 WScript.Sleep 100 Loop objIE.document.ParentWindow.ClipboardData.SetData "text", strTemp objIE.Quit
You’ll notice that the construct is the same. We first open an instance of Internet Explorer and then make use of it’s DOM to add the data to the clipboard. What you’ll notice different is the addition of the Do loop.
The Do loop acts as a sort of timer that waits to make sure that the application is fully initialized. This prevents any errors that might occur if you tried calling the SetData method before the DOM fully initialized. You could also apply this technique to the first example if you wanted to be thorough.
You now have a really nice workaround that will let you allow you to copy and paste data in the Windows clipboard through WSH. This can be a pretty handy workaround for temporarily saving information or for moving information between applications.
If you want to get or set binary data or if you don’t want to use Internet Explorer, there are many third-party ActiveX controls available. The downsides are that they would need to be installed on every machine that your script runs on and, to my knowledge, there aren’t any free ones available.