staticfish
Programmer
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
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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
[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]