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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

paste clipboard data into file

Status
Not open for further replies.

AzizKamal

Programmer
Apr 6, 2010
122
PK
I am trying to paste clipboard contents to a file with the following code:

Code:
<%
Dim sclpbrdtxt,fso,fr
Const ForReading = 1, ForWriting = 2

with createobject("internetexplorer.application")
    .navigate "about:blank"
    sclpbrdtxt=.document.parentwindow.clipboarddata.getdata("text")
    .quit
end with

Set fso = CreateObject("Scripting.FileSystemObject")
Set fr = fso.OpenTextFile("d:\test.txt", ForWriting, True)
'sclpbrdtxt="This is testing 123"
if isnull(sclpbrdtxt) then
       Response.Write "Hello One"
else
	Response.Write "Hello Two"
end if
'fr.WriteLine(sclpbrdtxt) 
'fr.close
%>

When this code is executed, I receive Hello One which means that contents are not assigned to sclpbrdtxt and test.txt remains blank.

If I assign a manual string for testing:

Code:
<%
Dim sclpbrdtxt,fso,fr
Const ForReading = 1, ForWriting = 2

with createobject("internetexplorer.application")
    .navigate "about:blank"
    sclpbrdtxt=.document.parentwindow.clipboarddata.getdata("text")
    .quit
end with

Set fso = CreateObject("Scripting.FileSystemObject")
Set fr = fso.OpenTextFile("d:\test.txt", ForWriting, True)
sclpbrdtxt="This is testing 123"
if isnull(sclpbrdtxt) then
	Response.Write "Hello One"
else
	Response.Write "Hello Two"
end if
fr.WriteLine(sclpbrdtxt) 
fr.close
%>

I receive Hello Two and test.txt file gets This is testing 123. I also tried wscript.sendkeys ("^v") but it did not paste contents into file
 
try something like this? would seem you need to wait? for IE readystate etc?

the code below is not mine, i do not have a ref to who authored it, credit where due

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

s=getcbstring()
wscript.echo s

function getcbstring()
'getcbstring is very much tempered,
'empty string is returned if it is unable to handle data or nothing is there.
set oie=createobject("internetexplorer.application")
oie.navigate "about:blank"
do while oie.readystate<>4 : wscript.sleep 50 : loop
set oclip=oie.document.parentwindow.clipboarddata
on error resume next
sdata=oclip.getdata("text")
if err.number=0 then
if lcase(typename(sdata))="string" then
getcbstring=sdata
else
getcbstring=""
end if
else
getcbstring=""
end if
on error goto 0
set oclip=nothing
oie.quit
set oie=nothing
end function
 
I checked for the readystate with the following code:

Code:
Dim sclpbrdtxt,fso,fr
Const ForReading = 1, ForWriting = 2

with createobject("internetexplorer.application")
    .navigate "about:blank"
	Response.Write .readystate & "<br>"
	'wscript.sleep 50
	do while .readystate<>4 : wscript.sleep 50 : loop
    sclpbrdtxt=.document.parentwindow.clipboarddata.getdata("text")
    .quit
end with

Set fso = CreateObject("Scripting.FileSystemObject")
Set fr = fso.OpenTextFile("d:\test.txt", ForWriting, True)
'sclpbrdtxt="This is testing 123"
if isnull(sclpbrdtxt) then
	Response.Write "Hello One"
else
	Response.Write "Hello Two"
end if
'fr.WriteLine(sclpbrdtxt) 
'fr.close

and I got the output:
4
Hello One

wscript.sleep is generating error. As a test, I tried this:
Code:
Dim sclpbrdtxt,fso,fr
Const ForReading = 1, ForWriting = 2

with createobject("internetexplorer.application")
    .navigate "about:blank"
	Response.Write .readystate & "<br>"
	wscript.sleep 50
	do while .readystate<>4 : wscript.sleep 50 : loop
    sclpbrdtxt=.document.parentwindow.clipboarddata.getdata("text")
    .quit
end with

Set fso = CreateObject("Scripting.FileSystemObject")
Set fr = fso.OpenTextFile("d:\test.txt", ForWriting, True)
'sclpbrdtxt="This is testing 123"
if isnull(sclpbrdtxt) then
	Response.Write "Hello One"
else
	Response.Write "Hello Two"
end if
'fr.WriteLine(sclpbrdtxt) 
'fr.close

and the following error occured:
Error Type:
Microsoft VBScript runtime (0x800A01F4)
Variable is undefined: 'wscript'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top