SendMessage/Sendkeys Questions
I'm not sure to use sendmessage or sendkeys with the following questions but anyways here they are:
1.) How can I use sendkeys/sendmessage to send all the text from a .txt file?
2.) How can I use sendkeys/sendmessage to send two backspaces and then the words "this is a test" and then the "enter" key?
Thanks.
Re: SendMessage/Sendkeys Questions
Quote:
Originally Posted by JBD2
I'm not sure to use sendmessage or sendkeys with the following questions but anyways here they are:
1.) How can I use sendkeys/sendmessage to send all the text from a .txt file?
Send all text from a text file where?
Quote:
Originally Posted by JBD2
2.) How can I use sendkeys/sendmessage to send two backspaces and then the words "this is a test" and then the "enter" key?
In order to do this, the appropriate code would need to be put into some type of event. Under what circumstances do you need to do this? Are you talking about a textbox? If so, what would trigger the SendKeys command? A click? A mousemove?
SendMessage is an API which can maniuplate messages used by a window.
SendKeys is a built in VB Function which permits certain keyboard sequences, or a single sequence, to be sent to a control or form.
Re: SendMessage/Sendkeys Questions
1) You would have to input the text from the text file into a string and then use SendMessage (hwnd of where to send,WM_SetText, len of string, string to send)
2) You would have to use:
VB Code:
SendKeys "{BS}"
SendKeys "{BS}"
SendKeys ("THIS IS A TEST")
SendKeys "{ENTER}"
-Sir Loin
Re: SendMessage/Sendkeys Questions
Both is to be sent to a textbox.
Re: SendMessage/Sendkeys Questions
How can I "input the text from the text file into a string"?
Re: SendMessage/Sendkeys Questions
Then why would you need SendMessage to send the text from a text file into the textbox? If you want the text of a text file to be in a text box, input the text into a String and then set the Textbox's Text property to that string.
Here's a BETTER example., the first was pretty crappy. :)
VB Code:
Option Explicit
Private Sub Form_Load()
Dim FF As Integer, StrText As String
FF% = FreeFile
Open "C:\TextFile.Txt" For Input As #FF
StrText$ = Input(LOF(FF), FF)
Close #FF
Text1.Text = StrText$
End Sub
-Sir Loin
Re: SendMessage/Sendkeys Questions
So, you have a text file that you want to load into a text box. Ok, try this:
VB Code:
Dim sfile As String
sfile = "c:\yourfile.txt"
Open sfile For Input As #1
Text1.Text = Input(LOF(1), #1)
Close #1
I'm still not completely sure what it is you want to do with the second part of your question, so I'll just give you some code, and you can place it in whatever event you wish.
VB Code:
'to use the enter key
SendKeys "{tab}"
'to use the backspace key
SendKeys "{backspace}"
Re: SendMessage/Sendkeys Questions
Ignore the second Question, I've got that figured out, but what I want to do for my first one is get all the text from a .txt file (let's say "C:\Myfile.txt"), and then using sendkeys or sendmessage, send the text from that text file. I just need this to be a function because I'm doing something else that checks (if, then etc...) so it checks if a window is open and if it is, it sends the text from that textfile. the window that is open has a textbox etc...Anyways all I need is the function that reads text from a text file and then sends it. Thanks.
Re: SendMessage/Sendkeys Questions
Quote:
Originally Posted by JBD2
Ignore the second Question, I've got that figured out, but what I want to do for my first one is get all the text from a .txt file (let's say "C:\Myfile.txt"), and then using sendkeys or sendmessage, send the text from that text file.
And this is what, after re-reading all of the posts in this thread, I still don't get...send it WHERE?
Re: SendMessage/Sendkeys Questions
As I said...to another textbox in another window, but I don't need that code. All I need is getting the text from the .txt file, and sending the text from it...lets say I made a string value called "textfromtxtfile", something like:
SendKeys "textfromtxtfile"
But if you want to know the class names of the textbox or whatever/window classname let me know.
Re: SendMessage/Sendkeys Questions
If it's not your app that you are sending the text to, you would need to know the handle of the textbox in question, and then use SendMessage to paste in text from a file. Is this what you want to do?
Re: SendMessage/Sendkeys Questions
Use Spy++ to get the classname of the textbox that you want to paste text in, then use SendMessage with WM_SETTEXT to paste the text.
Re: SendMessage/Sendkeys Questions
The thing is is it's a conversation window in MSN that I'm sending it to, and the whole window except for the title bar has the same classname...does that matter? Anyways I highlighted the text field though, it's still the same as the rest of the conversation window but anyways the classname is:
DirectUIHWND
and the handle is:
202E2
Can somebody give me a code example?
Re: SendMessage/Sendkeys Questions
I tried this:
VB Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Form_Load()
Dim sfile As String
sfile = "c:\test.txt"
Open sfile For Input As #1
SendMessage(DirectUIHWND, Input(LOF(1), #1), vbNullString, vbNullString) As Long
Close #1
'Input(LOF(1), #1)
End Sub
But it doesn't work.
Re: SendMessage/Sendkeys Questions
You have to read the file into a string variable, and then send the string.
Also, you can't use the classname, you have to use the hwnd of the textbox.
Re: SendMessage/Sendkeys Questions
Here, I'll give you an example. :)
VB Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SETTEXT = &HC
Private Const WM_CLOSE = &H10
Dim Notepad As Long
Private Sub Command1_Click()
Dim Text As Long
Notepad& = FindWindow("Notepad", vbNullString)
Text& = FindWindowEx(Notepad&, 0&, "edit", vbNullString)
Call SendMessage(Text&, WM_SETTEXT, Len(Text1.Text), ByVal Text1.Text)
End Sub
Private Sub Form_Load()
Shell "C:\WINDOWS\Notepad.exe", vbNormalNoFocus
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call SendMessage(Notepad&, WM_CLOSE, 0&, 0&)
End Sub
That will open Notepad, find the text area, and set it to what you have in a text box. Try it, you need a Command Button and a textbox.
-Sir Loin
Re: SendMessage/Sendkeys Questions
How do I modify that to get text from a text file?
Re: SendMessage/Sendkeys Questions
I posted an example on how to get text from a text file earlier, I'm sure you can figure out how to modify it yourself.
-Sir Loin
Re: SendMessage/Sendkeys Questions
Can you explain to me each of the parameters you used in SendMessage, and what they do?
Re: SendMessage/Sendkeys Questions
This was taken from AllAPI.net.
Quote:
The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread’s message queue and returns immediately.
VB4-32,5,6
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Operating Systems Supported
Requires Windows NT 3.1 or later; Requires Windows 95 or later
Library
User32
Parameter Information
· hWnd
Identifies the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
· Msg
Specifies the message to be sent.
· wParam
Specifies additional message-specific information.
· lParam
Specifies additional message-specific information.
Return Values
The return value specifies the result of the message processing and depends on the message sent.
-Sir Loin