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 strongm 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 1

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.
 
clipboard data is not being assigned to variable sclpbrdtxt. I need clipboard contents in this variable to write these contents to the file test.txt
 
[tt]with createobject("internetexplorer.application")
.navigate "about:blank"
[blue].document.parentwindow.clipboarddata.setdata "text", "This is testing 123"[/blue]
sclpbrdtxt=.document.parentwindow.clipboarddata.getdata("text")
.quit
end with[/tt]
 
When I tried the following code:

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

with createobject("internetexplorer.application")
    .navigate "about:blank"
	Response.Write .readystate & "<br>"
	.document.parentwindow.clipboarddata.setdata "text", "This is testing 123"
	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 received:
4
Hello Two

But I am setting clipboard data on one asp page and I have to get clipboard data on another asp page.

Page1.asp has the following code on onclick of a button:

Code:
<script language="Javascript">
function copyToClipboard() {
  s="This is clipboard data set from Page1";
  window.clipboardData.setData("Text",s);
  window.location = "[URL unfurl="true"]http://name/app/utilities/page2.asp"[/URL]
}
</script>

Now on page2.asp, I need to assign This is clipboard data set from Page1 to the variable sclpbrdtxt. But when I enter the following code on page2:

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

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

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

%>

and click the button on page1.asp, window location changes to page2.asp but I receive:

4
Hello One

When I manually open the notepad and press Ctrl+V, I get:
This is clipboard data set from Page1

but sclpbrdtxt is not receiving this value...
 
You have to do more studies on how web page serving works, conceptually at least - the talking stage, not real work. You use freely variables anywhere as if they have no scope, no life-cycle, no boundary, no nothing: just available anywhere any time. That won't do. The javascript part is on user's computer; the asp <% ... %> part is on the server (another computer, simply put). That is a huge jump. And that is also the reason why the question keeps changing in other's eyes and sounds so natural to your way of thinking as if that's the only meaning of the question.
 
page1.asp

Code:
<script type="text/javascript">

    function CopyToClipboard() {
       CopiedTxt = document.selection.createRange();
       CopiedTxt.execCommand("Copy");
    }

</script>

<form name="Form1" action="page2.asp" method="post">
Select (a part of) this text, and press the button...!<br /><br />

<input type="button" onClick="CopyToClipboard();submit();" value="Copy to clipboard" />


</form>


page2.asp
Code:
<script type="text/javascript">

    function PasteFromClipboard() {
        document.Form2.fClipboard.focus();
        PastedText = document.Form2.fClipboard.createTextRange();
        PastedText.execCommand("Paste");
    }

</script>


<body onload="PasteFromClipboard();">
<form name="Form2">
<input type="text" size="100" name="fClipboard" />


</form>
</body>


 
Thanks tsuji and foxbox.

execCommand is very useful in my scenario. I have to copy the whole page1.asp output to clipboard and my task is to send this output to excel. So earlier, I thought to copy the innerHTML of page1 body to clipboard, save it to a text file and then change the extension from txt to xls. But with execCommand, I have been able to achieve this so far:

page1.asp
Code:
<form name="Form1" action="page2.asp" method="post">
<a href="#" onclick="copytoclipboard();submit();">Click Me</a>

page1.asp
Code:
<script language="Javascript">
function copytoclipboard() 
{
this.document.execCommand("SelectAll", true); 
this.document.execCommand("Copy", true); 
this.document.execCommand("UnSelect", true); 
}
</script>

page2.asp
Code:
<%
Response.ContentType = "application/vnd.ms-excel"
%>

Clicking Click Me opens Excel and I can press Ctrl+V to view the contents. But as mentioned by tsuji, these clipboard contents are on client computer machine and writing them in a server script is an issue...
 
first put it in a form field
(new) page2.asp
Code:
<script type="text/javascript">

    function PasteFromClipboard() {
        document.Form2.fClipboard.focus();
        PastedText = document.Form2.fClipboard.createTextRange();
        PastedText.execCommand("Paste");
        document.Form2.submit();
    }

</script>


<body onload="PasteFromClipboard()">
<form name="Form2" action="page3.asp" method="post">
<input type="text" size="100" name="fClipboard" />


</form>

</body>


and use it in page3.asp
Code:
<%
response.Write "Clipboard value server side = " & request.Form("fClipboard")
%>


 
Thanks foxbox.

I tried this code. With input type="text", it showed me only first line in Excel. Then I changed this:

Code:
<input type="text" size="100" name="fClipboard" />

with this:
Code:
<textarea name="fClipboard" rows="2" cols="20"></textarea>

This worked OK, except that the pasted text in Excel is unformatted. Formatting is lost in this approach.

So, I ended up with this code:

page1.asp
Code:
<form name="Form1" action="page2.asp" method="post">
<a href="#" onclick="copytoclipboard();submit();">Click Me</a>

<script language="Javascript">
function copytoclipboard() 
{
s=document.body.innerHTML
window.clipboardData.setData("Text",s);
}
</script>

page2.asp
Code:
<form name="Form2" action="page3.asp" method="post">
<textarea name="fClipboard" rows="2" cols="20"></textarea>

<script type="text/javascript">

    function PasteFromClipboard() {
        document.Form2.fClipboard.focus();
        PastedText = document.Form2.fClipboard.createTextRange();
        PastedText.execCommand("Paste");
        document.Form2.submit();
    }

</script>

page3.asp
Code:
<%
dim fso,fw,f
Const ForReading = 1, ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set fw = fso.OpenTextFile("d:\test.txt", ForWriting, True)
fw.WriteLine(request.Form("fClipboard"))
fw.close
Set f = fso.GetFile("d:\test.txt")
f.Name = replace(f.Name, "txt", "xls") 
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top