How can I use Clipboard in vbscript? [duplicate]

You can do it with an html object to retrieve the contents of the clipboard:

' Get clipboard text
Set objHTML = CreateObject("htmlfile")
text = objHTML.ParentWindow.ClipboardData.GetData("text")

EDIT: I use this snippet to put text back on the clipboard, but it needs third party software; a standalone executable 'clip.exe' which can be found on Windows 2003 Server or just on the internet:

' Do something with the text
text = replace(text, "you ", "you and your dog ")

' Put it back to the clipboard
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")

Set oIn = oExec.stdIn

oIn.WriteLine text
oIn.Close

(Yes, it is all a little bit hackerdyhack)


VBScript doesn't support the clipboard. Most hosts that host vbscript, such as Internet Explorer give access through the host. Therefore vbscript running in IE or an HTA can use IE's clipboard support. The scripting hosts do not give clipboard support. You can use a vbs file to start IE through COM automation, navigate to a local page (to bypass security warnings), then use IE's clipboard.

Here's a code snippit (Outp. is a text stream)

    Set ie = CreateObject("InternetExplorer.Application") 
ie.Visible = 0
ie.Navigate2 "C:\Users\David Candy\Desktop\Filter.html"
Do 
    wscript.sleep 100
Loop until ie.document.readystate = "complete"  
txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
ie.quit
If IsNull(txt) = true then 
    outp.writeline "No text on clipboard"
else
    outp.writeline txt
End If