Use clipboard from VBScript

后端 未结 15 1412
青春惊慌失措
青春惊慌失措 2020-11-29 06:36

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

相关标签:
15条回答
  • 2020-11-29 07:25

    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
    
    0 讨论(0)
  • 2020-11-29 07:27

    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
    
    0 讨论(0)
  • 2020-11-29 07:27

    If it's just text can't you simply create a text file and read in the contents when you need it?

    Another alternative and clearly a kludge, would be to use the SendKeys() method.

    0 讨论(0)
提交回复
热议问题