Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

scraping text from the active window

195 views
Skip to first unread message

geoffrey pritchard tillingsley

unread,
Jul 20, 2003, 11:21:05 AM7/20/03
to
I'd like to be able to have excel scrape text from an active window
while I'm surfing. I can currently import a picture, but would prefer
the text.

I do a snapshot using the following code:

Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Const VK_SNAPSHOT = &H2C
Public Const KEYEVENTF_KEYUP = &H2


Sub screenshot()

For i = 1 To 10000
For j = 1 To 10000
Next
Next
keybd_event VK_SNAPSHOT, 1, 0, 0
Range("a5").Select
ActiveSheet.Paste

Application.ScreenUpdating = True

End Sub

The reason for the loop is to give me time to click on the desired
window after I start the macro.

My thinking is there is a possibility that

keybd_event VK_SNAPSHOT, 1, 0, 0

Could be changed to scrape the text instead of the snapshot.

I understand a website can be scraped, but prefer not to do it this
way. And I know I can hit control A, control C and control V in the
spreasheet, but prefer not to do it that way.

Thanks,

Geoffrey Pritchard Tillingsley

Nate Oliver

unread,
Jul 20, 2003, 7:45:38 PM7/20/03
to
Hello Geoffrey,

Typically you want to set the object and grab the inner
text. I'd use an array and split it on VBCRLF.

So if you want to grab text from a non-framed window, like
msn.com, try something like:

Sub Download_Unread1()
Dim ie As Object, frm As Object, myArr
Set ie = GetObject(, "InternetExplorer.Application")
myArr = Split(ie.document.body.innerText, vbCrLf)
[a5].Resize(UBound(myArr) + 1) = Application.Transpose
(myArr)
Set ie = Nothing
End Sub

You'll need to tweak this for frames, i.e., a google
newsgroup thread:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-
8&threadm=01bb9ff3%244a93f360%24LocalHost%
40dpmellon&rnum=1&prev=/groups%3Fq%3D%2522I%2Bhate%
2BAccess%2522%26hl%3Den%26lr%3D%26ie%3DUTF-8%26selm%
3D01bb9ff3%25244a93f360%2524LocalHost%2540dpmellon%26rnum%
3D1

Sub Download_Unread2()
Dim ie As Object, frm As Object, myArr
Set ie = GetObject(, "InternetExplorer.Application")
'0-Based, 0 grabs left frame, 1 grabs right frame.
myArr = Split(ie.document.frames
(1).document.body.innerText, vbCrLf)
[a5].Resize(UBound(myArr) + 1) = Application.Transpose
(myArr)
Set ie = Nothing
End Sub

Hope this helps.

Regards,
Nate Oliver

Nate Oliver

unread,
Jul 20, 2003, 7:55:38 PM7/20/03
to
Hmmm, it appears I had a garbage variable in both
procedures, let's try:

Sub Download_Unread2()
Dim ie As Object, myArr As Variant


Set ie = GetObject(, "InternetExplorer.Application")
'0-Based, 0 grabs left frame, 1 grabs right frame.
myArr = Split(ie.document.frames
(1).document.body.innerText, vbCrLf)
[a5].Resize(UBound(myArr) + 1) = Application.Transpose
(myArr)
Set ie = Nothing
End Sub

Sub Download_Unread1()
Dim ie As Object, myArr As Variant


Set ie = GetObject(, "InternetExplorer.Application")
myArr = Split(ie.document.body.innerText, vbCrLf)
[a5].Resize(UBound(myArr) + 1) = Application.Transpose
(myArr)
Set ie = Nothing
End Sub

And a shorter test link:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-
8&threadm=50uon0%24gte%40falcon.ns.net&rnum=1&prev=/groups%
3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26selm%3D50uon0%2524gte%
2540falcon.ns.net

Sorry 'bout that. Regards,
Nate Oliver

0 new messages