Use clipboard from VBScript

后端 未结 15 1446
青春惊慌失措
青春惊慌失措 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: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
    

提交回复
热议问题