Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS to Copy Field Value to Clipboard 1

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
US
I have users working in two applications. They have to copy a driver's license value from App1 and paste it into App2. However, App2 only accepts the number portion of the driver's license, not the (first two characters) state abbreviation. My users would like a way to just copy all but the first two characters without having to select it with the mouse because of user error in making the selection. Doesn't seem like a big deal, but it takes each user a couple seconds to copy. Multiply that by 50 users and 2,000+ records per day and it starts to impede productivity.

App1 has the capability to run external programs, such as .vbs files. I'd like to figure out a way using vbs to copy the applicable portion of the value (by dropping the first two characters) to the clipboard. From the research I've done, it looks like this can be done with the window.clipboardData.setData sub, but I'm not sure how to set the variable up to extract the data (without the first two characters) from the field.

Any help would be greatly appreciated.

stinsman
 
Yes you can do this but you will need to install WSHExtra.dll on the client PC.


Once you have WSHExtra you are able to copy to the clipboard. You could easily have it strip the first two characters before sending to the clipboard using the Right Function.

Example:
Right(string,(Len(string)-2)).

I hope you find this post helpful.

Regards,

Mark
 
I registered the .dll and I'm trying to modify the "Listing_01.CopyClipboard.vbs" code to accommodate my needs, but I'm having trouble with it.

This is what I've got:
Code:
strArg = WScript.Arguments(0)
If strArg = "" Then
MsgBox "There is no driver's license to be copied", vbOkOnly+vbExclamation+vbSystemModal, "Driver's License"

Else

Dim clip
Set clip = CreateObject _
	("WshExtra.Clipboard")

strText = clip.Paste()
	Set fso = CreateObject _
	("Scripting.FileSystemObject")
	Set f = fso.CreateTextFile("note.txt")
	f.Write strText
	f.Close
End If

I'm know I'm way off here, but I don't know how to use this code. When I run this, nothing (apparently)happens.

How does the WshExtra.dll use the "note.txt" file? Do I just need to add that file to the C:\WINDOWS\System32 directory?

The field specified strArg is provided by an "Arguments" field in the application.
 
The example you have postged assumes that the user has already copied the value to the clipboard.

Since you are looking to strip the first two characters here is how you will want to do it.

Code:
Dim clip
Set clip = CreateObject _
	("WshExtra.Clipboard")

Dim strText
strText = clip.Paste()

ShortTxt = Right(strText,(Len(strText)-2))

If strText <> "" Then
	msgbox ShortTxt
End If

I hope you find this post helpful.

Regards,

Mark
 
And here is an example of how to take a variable or string and SEND it to the clipboard and then later paste it back out. Note that it strips out the leading two digits as you requested.

Code:
Dim clip
Set clip = CreateObject _
	("WshExtra.Clipboard")

Dim strText
Clip.Copy("001234hello")
strText = clip.Paste()

ShortTxt = Right(strText,(Len(strText)-2))

If strText <> "" Then
	msgbox ShortTxt
End If

I hope you find this post helpful.

Regards,

Mark
 
Mark, thanks for your help on this. That second one is what I need. But it doesn't seem to do what I'm looking for. I need for the script to copy the value to the clipboard and not paste it into a msgbox, but enable the user to manually paste it into another program when they're ready.

Seems like this should work, but it doesn't:
Code:
strArg = WScript.Arguments(0)

If strArg = "" Then
MsgBox "There is no driver's license to be copied", vbOkOnly+vbExclamation+vbSystemModal, "Driver's License"

Else

Dim clip
Set clip = CreateObject _
    ("WshExtra.Clipboard")

Clip.Copy(Right(strArg,(Len(strArg)-2)))

End If
 
not my own work, not sure where it came from, works for me (i.e. i run the script then open notepad and do a cntrol-c and the text is there) but requires IE,

sText = "some text to clipboard"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
Do Until objIE.ReadyState=4: WScript.Sleep 1: Loop
objIE.Document.ParentWindow.ClipboardData.SetData "Text", sText
objIE.Quit
 
Thanks guys for your help and feedback!

I've got to test it out a bit, but I think the option mrmovie provided will work out best because the only requirement is that IE is installed, which is an absolute in my environment.

stinsman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top