views:

8051

answers:

5

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

+3  A: 

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

Zoredache
+2  A: 

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

Zoredache
+2  A: 

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

unrealtrip
A: 

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.

tloach
A: 

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.

Jeffery Hicks