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!

Sendmessage API 1

Status
Not open for further replies.

Elysium

Programmer
Aug 7, 2002
212
US
I need some guidance on selecting all text in an IE window, copying it to the clipboard and then pasting it to notepad. This should all be done from a class residing in a seperate VB program. For the life of me, I can't seem to figure it out. I can use SendKeys easily, but that's too problematic for me. There just has to be a way to do this using Sendmessage or Postmessage.

Has anyone successfully done this?

Regards,



Randy
[afro]
 
There are better ways to do this. This one is an example of getting an already loaded web page, copying the inner text to the clipboard and posting it to an empty already opened Notepad. It's a module using a Sub Main.

I have to question whether or not you might want to just create a text file or open an existing one for append instead of using Post Message. I'm not sure what you are trying to do but here is an example.

Code:
Option Explicit


Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long

Declare Function EnumChildWindows Lib "user32" _
(ByVal hWndParent As Long, ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long

Declare Function GetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, ByVal cch As Long) As Long

Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" _
(ByVal hwnd As Long) As Long

Declare Function FindWindow Lib "user32.dll" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Const WM_SETTEXT = 12
Const WM_CHAR = &H102
Private X As String

Sub Main()
'Add a reference  to Microsoft Internet Controls
 'to your project
 

Dim SHDocVw As New ShellWindows
Dim IE As InternetExplorer
Dim h As Long


For Each IE In SHDocVw
If IE.LocationURL = _
"[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=946898&page=1"[/URL] Then
 X = IE.Document.body.innertext
End If
Next

Clipboard.SetText X

h = FindWindow(vbNullString, "Untitled - Notepad")
EnumChildWindows h, AddressOf EnumChildProc, 1

End Sub




Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sSave As String
Dim i As Long, g$, X As String
sSave = Space$(GetWindowTextLength(hwnd) + 1)
GetWindowText hwnd, sSave, Len(sSave)
sSave = Left$(sSave, Len(sSave) - 1)
X = Clipboard.GetText

For i = 1 To Len(X)
g = Asc(Mid(X, i, 1))
PostMessage hwnd, WM_CHAR, ByVal g, 393217
Next
Exit Function
'continue enumeration
EnumChildProc = 1
End Function
 
OOPs. I had Const WM_SETTEXT = 12 in there from another project I am working on. That can be removed.
 
Mosaic1,

Your approach is the one I took and it works very well. Thank you for the response and code example.

Regards,

Randy
[afro]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top