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

How to capture text selected in IE browser using VBA 2

Status
Not open for further replies.

staticfish

Programmer
Jun 29, 2006
4
FR

Hi folks,

I am trying to acquire the text selected in a web browser(IE)
using VBA. Once I am able to do that I want to search that text in a dictionary. Can somebody help me on this.

Regards
 
staticfish,
Here is rough sample that should get you moving in the right direction. I can't remember how to hook an existing instance of Internet Explorer (IE) so this sample spawns it's own instance.
I used a [tt]Stop[/tt] statement to allow you select some text in the IE window before it is returned to the routine. In the real world you would use a global object or class to allow the user to select text from the IE window.
Code:
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

Sub TestCopy()
Dim objIE As Object 'SHDocVw.InternetExplorer
Dim strClipboard As String
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "[URL unfurl="true"]http://finance.yahoo.com/"[/URL]
objIE.Visible = True
Stop

'This is where you can navigate to IE and select some text

objIE.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
strClipboard = ClipBoard_GetData
objIE.Quit
Set objIE = Nothing
Debug.Print strClipboard
End Sub

Function ClipBoard_GetData()
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim MyString As String
Dim RetVal As Long
If OpenClipboard(0&) = 0 Then
    MsgBox "Cannot open Clipboard. Another app. may have it open"
    Exit Function
End If

' Obtain the handle to the global memory block that is referencing the text.
hClipMemory = GetClipboardData(CF_TEXT)
If IsNull(hClipMemory) Then
    MsgBox "Could not allocate memory"
    GoTo Clean_Up
End If

' Lock Clipboard memory so we can reference the actual data string.
lpClipMemory = GlobalLock(hClipMemory)
If Not IsNull(lpClipMemory) Then
    MyString = Space$(MAXSIZE)
    RetVal = lstrcpy(MyString, lpClipMemory)
    RetVal = GlobalUnlock(hClipMemory)
    ' Peel off the null terminating character.
    MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
Else
    MsgBox "Could not lock memory to copy string from."
End If
Clean_Up:
RetVal = CloseClipboard()
ClipBoard_GetData = MyString
End Function

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Hmm - you might find (assuming ObjIE has the correct IE object) that:
Code:
[blue]Debug.Print ObjIE.Document.selection.createRange.Text[/blue]

is a little simpler ...
 
Hi CMP,

Thanks for your reply it works. Sorry for the late reply.

Regards
 
Sure, but is complete overkill and unneccessary given that, as I showed, the IE object can return the selected text directly.

CMP's code could therefore be rewritten in its entirety as:
Code:
[blue]Sub TestCopy()
Dim objIE As Object [green]'SHDocVw.InternetExplorer[/green]
Dim strClipboard As String
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "[URL unfurl="true"]http://finance.yahoo.com/"[/URL]
objIE.Visible = True
Stop

[green]'This is where you can navigate to IE and select some text[/green]

Debug.Print objIE.Document.selection.createRange.Text
End Sub[/blue]
 

Hi Strongm,

Thanks for the help. It works. I was also wondering why you need to play with clipboard and all and why the ie object just can return it simply.

Thanks again.

Regards
 
staticfish,
You don't need to play with the clipboard. I showed you one way, [blue]strongm[/blue] showed you a better way. I would go with his solution because it's simpler.

CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Hi,

Continuing on my efforts to capture text from IE now I am checking if a particular webpage is already open, if it is open then display text selected on that page in the immediate window. the code I have written is as follows.

Private Sub Copy()
Dim objIE As SHDocVw.InternetExplorer
Dim objSW As SHDocVw.ShellWindows
Dim htmObj As HTMLDocument
Dim sel As Object
Dim flag As Boolean

Dim shw As New ShellWindows

Set objSW = New ShellWindows



'If objIE.Document.Selection.createRange.Text <> Null Then
If objSW.Count Then
For Each objIE In objSW
If InStr(1, objIE.LocationName, "Yahoo!", vbTextCompare) Then
flag = True
Debug.Print "Found"
Set objIE = New InternetExplorer
Set objIE = objSW.Item
objIE.Visible = True
Stop
End If
Debug.Print objIE.Document.Selection.createRange.Text
Next objIE
End If

End Sub

Here for the line
Debug.Print objIE.Document.Selection.createRange.Text

it gives following error.


---------------------------
Microsoft Visual Basic
---------------------------
Run-time error '438':

Object doesn't support this property or method
---------------------------
OK Help
---------------------------

I have written this procedure in "This Workbook" of an excel. Can anybody put some light on this, becuase it works in following code suggested by strongm.

Sub TestCopy()
Dim objIE As Object 'SHDocVw.InternetExplorer
Set objIE = CreateObject("InternetExplorer.Application")
'Set objIE = GetObjectobjIE.Navigate "objIE.Visible = True
Stop

'This is where you can navigate to IE and select some text
'Debug.Print objIE.Document.Selection.createRange.Text

Debug.Print objIE.Document.Selection.createRange.Text

End Sub

Thanks & Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top