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条回答
相关推荐>>
2楼-- · 2019-01-09 03:59

Microsoft doesn't give a way for VBScript to directly access the clipboard. If you do a search for 'clipboard'on this site you'll see:

Although Visual Basic for Applications supports the Screen, Printer, App, Debug, Err, and Clipboard objects, VBScript supports only the Err object. Therefore, VBScript does not allow you to access such useful objects as the mouse pointer or the clipboard. You can, however, use the Err object to provide runtime error handling for your applications.

So using notepad indirectly is probably about the best you'll be able to do with just VBScript.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-09 03:59

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

http://www.petri.co.il/quickly_copy_error_and_display_messages.htm

Example with a multiline string as per question below

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
查看更多
冷血范
4楼-- · 2019-01-09 04:02

I've found a way to copy multi line information to clipboard by vbscript/cmd.

Sequence:

  • with VBS generate the final "formatted string" that you need copy to clipboard
  • generate a (txt) file with the "formatted string"
  • use type command from cmd to paste information to clip by pipe

Example script:

Function CopyToClipboard( sInputString )

    Dim oShell: Set oShell = CreateObject("WScript.Shell")
    Dim sTempFolder: sTempFolder = oShell.ExpandEnvironmentStrings("%TEMP%")
    Dim sFullFilePath: sFullFilePath = sTempFolder & "\" & "temp_file.txt"

    Const iForWriting = 2, bCreateFile = True
    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    With oFSO.OpenTextFile(sFullFilePath, iForWriting, bCreateFile)
        .Write sInputString
        .Close
    End With

    Const iHideWindow = 0, bWaitOnReturnTrue = True
    Dim sCommand: sCommand = "CMD /C TYPE " & sFullFilePath & "|CLIP"
    oShell.Run sCommand, iHideWindow, bWaitOnReturnTrue

    Set oShell = Nothing
    Set oFSO = Nothing

End Function

Sub Main

    Call CopyToClipboard( "Text1" & vbNewLine & "Text2" )

End Sub

Call Main
查看更多
孤傲高冷的网名
5楼-- · 2019-01-09 04:02

I devised another way to use IE and yet avoid security warnings...

By the way.. this function is in JavaScript.. but u can easily convert it to VBScript..

function CopyText(sTxt) {
    var oIe = WScript.CreateObject('InternetExplorer.Application');
    oIe.silent = true;
    oIe.Navigate('about:blank');
    while(oIe.ReadyState!=4) WScript.Sleep(20);
    while(oIe.document.readyState!='complete') WSript.Sleep(20);
    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>";
    var oTb = oIe.document.getElementById('txtArea');
    oTb.value = sTxt;
    oTb.select();
    oTb = null;
    oIe.ExecWB(12,0);
    oIe.Quit();
    oIe = null;
}
查看更多
小情绪 Triste *
6楼-- · 2019-01-09 04:08

Here's another version of using the "clip" command, which avoids adding a carriage return, line feed to the end of the string:

strA= "some character string"

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /C echo . | set /p x=" & strA & "| c:\clip.exe", 2

s = "String: """ & strA & """ is on the clipboard."
Wscript.Echo s

I've only tested this in XP. clip.exe was downloaded from http://www.petri.co.il/downloads/clip.zip and placed in C:\ .

查看更多
戒情不戒烟
7楼-- · 2019-01-09 04:09

The easiest way is to use built-in mshta.exe functionality:

sText = "Text Content"
CreateObject("WScript.Shell").Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(sText, "\", "\\"), "'", "\'") & "');close();""", 0, True

To put to clipboard a string containing double quote char ", use the below code:

sText = "Text Content and double quote "" char"
CreateObject("WScript.Shell").Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(Replace(sText, "\", "\\"), """", """"""), "'", "\'") & "'.replace('""""',String.fromCharCode(34)));close();""", 0, True
查看更多
登录 后发表回答