I need to browse to a web page, select all on the page and copy it to the
clipboard with VBScript. I've been unsuccessful trying to use clipboardData,
sendkeys, or execCommand. Is there a way to do this?
Thanks in advance.
with createobject("internetexplorer.application")
.navigate "www.yahoo.com"
do until .readystate = 4 : wscript.sleep 10 : loop
.document.body.createtextrange.select
.document.execCommand "copy"
.quit
end with
msgbox "ready to paste..."
--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
Thank you for the excellent hint. Used it immediately in VB6 and got
it to work as follows:
With CreateObject("internetexplorer.application")
.Navigate "http://www.yahoo.com"
Do Until .ReadyState = 4: Loop
.Document.body.createtextrange.Select
.Document.execCommand "copy"
.Quit
End With
MsgBox "ready to paste..."
But could someone please help with the following two problems:
1. It is easy to copy from a web page with text between <body>
<body>/ but when there are no bodies only <frameset> <frameset>/ how
do you get copy then. At any case I didn't succeed?.
2. You want to copy areas on a page that are protected by username and
password - how to log in when you already have the page open?.
If anyone has ideas please tell.
Thanks in advance
This might be a little less CPU intensive...
Do Until .ReadyState = 4: DoEvents : Loop
> .Document.body.createtextrange.Select
> .Document.execCommand "copy"
> .Quit
> End With
>
> 1. It is easy to copy from a web page with text between <body>
> <body>/ but when there are no bodies only <frameset> <frameset>/ how
> do you get copy then. At any case I didn't succeed?.
You have walk the frames collection looking for the frame (window) than has
the document.body element that you want to copy from...
>
> 2. You want to copy areas on a page that are protected by username and
> password - how to log in when you already have the page open?.
>
??? How is an area of the page (as opposed to the page itself)
username/password protected?
How you would log in depends entirely on how the specific page implements
log in...
Thanks for the answer it helped me half the way.
1. There was only one "?" missing causing the whole problem.
2. Still exist the second problem to sign in.E.g. when choose
www.pcmag.com you get the front page. If you want to have a look to
your own profile it is necessary to log in.I have tried it by putting
username and password to URL as follows:
This is the page when I view my own profile by logging in from
keyboard
.Navigate "http://www.pcmag.com/view_profile/0,2993,,00.asp
And this is the verision as I try directly log in
.Navigate "http://www.pcmag.com/view_profile/0,2993,,00.asp/maillogin?txt_user_name=someuser&txt_user_pwd=pasw"
txt_user_name and txt_user_pwd I took from pcmag*s page so they should
be wrigth. I only get the message that page not exist. On some other
pages get message pls give your username and password.
What is the wright way automatically to send userid and password?
Thanks
I'd suggest navigating to a page that has the login form, access the DOM of
that page, access the form/form elements and assign the username/password,
then call the form's submit method or the submit button's click method.
Yes - it is much easier to find something when you know what to search
for. Thank you for your advice. Found here in Google the following
solution:
(sorry don't now remember who has written following code)
First with WebBrowser navigate to site,
then
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As
Variant)
Dim oColl As IHTMLElementCollection
' pDisp is an interface pointer to the dom of page, oCOll is the
collection of tags on our page
If pDisp Is Nothing Then Return
Set oColl = pDisp.Document.documentElement.All.tags("INPUT")
Debug.Print oColl.length
On Error GoTo ErrHandler:
oColl("username").Value = "somebody"
oColl("password").Value = "secret"
oColl("login").Click
ErrHandler:
On Error Resume Next
End Sub
So now we have landed excatly to the page I wanted to. Now I want to
do following:
copy part or the whole page to excel. Have read DOM info in MSDN but
somehow don't understand how to get control over the page we are now.
How to manipulate tags on that page. Is anyone willing to assist here
a little bit?
Thanks
If I were doing this fron VB (as you appear to be)...
(Note: untested air-code, but I think I got the syntax right ;-)
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As
Variant)
If pDisp Is Nothing Then Exit Sub '??? seems unlikely to happen
On Error GoTo ErrHandler:
With pDisp.Document.Forms.Item("formname").Elements
.Item("username").Value = "somebody"
.Item("password").Value = "secret"
.Item("login").Click
End With
Exit Sub
ErrHandler:
With Err
MsgBox .Number & " " & .Description
End With
End Sub