Use clipboard from VBScript

2019-01-09 03:42发布

I am looking for a method to place some text onto the clipboard with VBScript. The VBScript in question will be deployed as part of our login script. I would like to avoid using anything that isn't available on a clean Windows XP system.

Edit: In answer to the questions about what this is for.

We wanted to encourage users inside our organization to use the file server to transfer documents instead of constantly sending attachments by email. One of the biggest barriers to this is that it isn't always obvious to people what the correct network path is to a file/folder. We developed a quick script, and attached it to the Windows context menu so that a user can right click on any file/folder, and get a URL that they can email to someone within our organization.

I want the URL displayed in the dialog box to also be placed onto the clipboard.

GetNetworkPath

15条回答
ら.Afraid
2楼-- · 2019-01-09 04:10

Here is Srikanth's method translated into vbs

function SetClipBoard(sTxt)
    Set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.value = sTxt
    oTb.select
    set oTb = nothing
    oIe.ExecWB 12,0
    oIe.Quit
    Set oIe = nothing
End function


function GetClipBoard()
    set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop 

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.focus   
    oIe.ExecWB 13,0
    GetClipBoard = oTb.value
    oTb.select
    set oTb = nothing
    oIe.Quit
    Set oIe = nothing
End function
查看更多
smile是对你的礼貌
3楼-- · 2019-01-09 04:10

Take a look at this post. It describes a hacky approach to read from the clipboard, but I imagine it could be adapted to also write to the clipboard as well, such as changing the Ctrl+V to Ctrl+A then Ctrl+C.

查看更多
走好不送
4楼-- · 2019-01-09 04:12

No security warnings and no carriage return at the end of line

' value to put in Clipboard
mavaleur = "YEAH"

' current Dir
path = WScript.ScriptFullName
GetPath = Left(path, InStrRev(path, "\"))

' Put the value in a file
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile=GetPath & "fichier.valeur"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write mavaleur
objFile.Close

' Put the file in the Clipboard
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c clip < " & outFile, 0, TRUE

' Erase the file
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile outFile
查看更多
登录 后发表回答