How can I use Clipboard in vbscript? [duplicate]

泪湿孤枕 提交于 2019-11-27 04:56:31

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
AutomatedChaos

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)

You need this function (is a little modification of this):

'TO CLEAR
ClipBoard("")

'TO SET
ClipBoard("Hello World!")

'TO GET
Result = ClipBoard(Null)

Function ClipBoard(input)
'@description: A quick way to set and get your clipboard.
'@author: Jeremy England (SimplyCoded)
  If IsNull(input) Then
    ClipBoard = CreateObject("HTMLFile").parentWindow.clipboardData.getData("Text")
    If IsNull(ClipBoard) Then ClipBoard = ""
  Else
    CreateObject("WScript.Shell").Run _
      "mshta.exe javascript:eval(""document.parentWindow.clipboardData.setData('text','" _
      & Replace(Replace(Replace(input, "'", "\\u0027"), """","\\u0022"),Chr(13),"\\r\\n") & "');window.close()"")", _
      0,True
  End If
End Function

For the equivalent of a "paste" operation I would run a command-line utility like ClipOut or paste, redirect output to a file and read the file contents.

return = WshShell.Run("cmd /c clipout.exe > output.txt", 0, true)

Set fso  = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("output.txt", 1)
text = file.ReadAll
file.Close

You can get ClipOut here: http://jasonfaulkner.com/ClipOut.aspx

You can get paste here: https://www.c3scripts.com/tutorials/msdos/paste.html

For the equivalent of a "copy" operation I would use the clip command line utility that actually comes with Windows and similar code as above.

About the clip utility: https://blogs.msdn.microsoft.com/oldnewthing/20091110-00/?p=16093

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!