Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use clipboard from VBScript

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

like image 918
Zoredache Avatar asked Sep 24 '08 17:09

Zoredache


2 Answers

Another solution I have found that isn't perfect in my opinion, but doesn't have the annoying security warnings is to use clip.exe from a w2k3 server.

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE

Example with a multiline string as per question below : Link1

Dim string
String = "text here" &chr(13)& "more text here"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo " & String & " | clip", 0, TRUE
like image 164
Zoredache Avatar answered Oct 16 '22 07:10

Zoredache


Using Microsoft's clip.exe is the closest to having a clean Windows XP system solution. However you don't have to call CMD.EXE to host it in order to use it. You can call it directly and write to its input stream in your script code. Once you close the input stream clip.exe will write the contents straight to the clipboard.

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")
Set oIn = oExec.stdIn
oIn.WriteLine "Something One"
oIn.WriteLine "Something Two"
oIn.WriteLine "Something Three"
oIn.Close

If you need to wait for clip to be finished before your script can continue processing then add

' loop until we're finished working.
Do While oExec.Status = 0
    WScript.Sleep 100
Loop

And don't forget to release your objects

Set oIn = Nothing
Set oExec = Nothing
like image 15
klaatu Avatar answered Oct 16 '22 05:10

klaatu