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:54

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
查看更多
一夜七次
3楼-- · 2019-01-09 03:54

In your Class ClipBoard, neither the Clear sub nor the Let Data sub work. I mean they have no effect on Windows Clipboard. Actually, and ironically so, the only sub that works is the one you have not included in your example, that is Get Data! (I have tested this code quite a few times.)

However, it's not your fault. I have tried to copy data to clipboard with ClipboardData.SetData and it's impossible. At least not by creating an "htmlfile" object. Maybe it works by creating an instance of "InternetExplorer.Application" as I have seen in a few cases, but I have not tried it. I hate creating application instances for such simple tasks!

Alkis

查看更多
姐就是有狂的资本
4楼-- · 2019-01-09 03:54

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.

查看更多
倾城 Initia
5楼-- · 2019-01-09 03:55

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

查看更多
男人必须洒脱
6楼-- · 2019-01-09 03:55

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
查看更多
淡お忘
7楼-- · 2019-01-09 03:56

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

查看更多
登录 后发表回答