Use clipboard from VBScript

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

    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
    
    0 讨论(0)
  • 2020-11-29 07: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 Link and placed in C:\.

    0 讨论(0)
  • 2020-11-29 07:09

    No security warnings, full let and get access:

    'create a clipboard thing
     Dim ClipBoard
     Set Clipboard = New cClipBoard
    
     ClipBoard.Clear  
     ClipBoard.Data = "Test"
    
    Class cClipBoard
            Private objHTML
    
                    Private Sub Class_Initialize
                            Set objHTML = CreateObject("htmlfile")
                    End Sub
    
                    Public Sub Clear()
                            objHTML.ParentWindow.ClipboardData.ClearData()
                    End Sub
    
                    Public Property Let Data(Value)
                            objHTML.ParentWindow.ClipboardData.SetData "Text" , Value
                    End Property
    
                    Public Property Get Data()
                            Data = objHTML.ParentWindow.ClipboardData.GetData("Text")
                    End Property
    
                    Private Sub Class_Terminate
                            Set objHTML = Nothing
                    End Sub
    
    End Class
    

    Example Usage.

    ' Create scripting object
    Dim WShell, lRunUninstall
    Set WShell = CreateObject("WScript.Shell")
    WShell.sendkeys "^c"
    WScript.Sleep 250
    bWindowFound = WShell.AppActivate("Microsoft Excel")
     WShell.sendkeys ClipBoard.Data
    
    0 讨论(0)
  • 2020-11-29 07:12

    The closest solution I have found so far is a method to use IE to get and set stuff on the clipboard. The problem with this solution is the user gets security warnings. I am tempted to move 'about:blank' to the local computer security zone so I don't get the warnings, but I am not sure what the security implications of that would be.

    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Navigate("about:blank")
    objIE.document.parentwindow.clipboardData.SetData "text", "Hello This Is A Test"
    objIE.Quit
    

    http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec04/hey1215.mspx

    0 讨论(0)
  • 2020-11-29 07:18

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

    To avoid the security warnings associated with Internet Explorer and clipboard access, I would recommend you use the Word application object and its methods to put your data onto the clipboard. Of course you can only use this on a machine that has MS Word installed, but these days that's most of them. (*In spite of the fact that you asked for stuff on a 'clean' system :) *)

    ' Set what you want to put in the clipboard '
    strMessage = "Imagine that, it works!"
    
    ' Declare an object for the word application '
    Set objWord = CreateObject("Word.Application")
    
    ' Using the object '
    With objWord
       .Visible = False         ' Don't show word '
       .Documents.Add           ' Create a document '
       .Selection.TypeText strMessage   ' Put text into it '
       .Selection.WholeStory        ' Select everything in the doc '
       .Selection.Copy          ' Copy contents to clipboard '
       .Quit False          ' Close Word, don't save ' 
    End With
    

    You can find detail on the MS Word application object and its methods here: http://msdn.microsoft.com/en-us/library/aa221371(office.11).aspx

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