It works BUT it opens the .exe file twice then sometimes it fills the fields in on just one form and other times it splits the message across the two. I've tried putting the code in CmdSend mousedown, mouseup, pressdown ect to see if that helps but even tho it does only bring up the form once it is minimized rather than on the creen with the focus set
Any ideas on how to make the .exe file open just once?
You should check if the program windows has been launched before using the sendkeys.
So after shell():
Use the FindWindow() api to check if the window is lanuched and then try send keys.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Sub CmdSend_Click()
Dim retval
Dim hWnd As Long
retval = Shell("C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", 1)
hWnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")
If hWnd > 0 Then
SendKeys "abc", 1
End If
BUT... Even tho it s now bringing up just the one form like I want I'm getting the error: Type Mismatch on this line of code: hWnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")
The name i.e. "Nokia Text Message Editor - Connected to Nokia 6300" is what the form itself is called (says so at top of the form and on my menu bar)
Any ideas?
Hmmm, your API declaration is expecting LONG string pointers, try simply changing them to String parameter types
If using Strings in the API call, change the delcaration parameters to be String not Long, else change your calls to use string pointers.
Code:
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Last edited by LaVolpe; Mar 22nd, 2008 at 11:24 AM.
Insomnia is just a byproduct of, "It can't be done"
Using VaLopes Line of code I no longer get the error BUT now it won't write to the form anymore
If I put the code for SendKeys here:
If hWnd > 0 Then
'IFS IT OPEN
SendKeys "abc", 1
SendKeys "{TAB}", 1
SendKeys "Your mesage here", 1
End If
It doesn't write at all but if I write the three SendKeys lines under the 'end if' then I have the same problem of it bringing up the form twice instead of once
Update the hWnd isn't working because when I change it to:
if hWnd = 0 Then
so equal to zero rather than more than it then goes back to writting in the forms but again bringing up two of them
EDIT:
I've done a step through with the code and it keeps saying hWnd = 0 even though at that point it is suppose to be reconising this line:
hWnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")
I've tried changing from the form name to the path and TestMessageEditor.exe but it doesn't reconise any
So now I'm even more confused than I was an hour ago
When using the findwindow fuction am I suppose to be adding anything from the library, list of conponants or references??? What I mean is there is nothing extra to do is there apart from just writing the line of code to my existing code?
Last edited by bubblegum_girl; Mar 22nd, 2008 at 12:08 PM.
Good catch on your part regarding the hWnd>0 part. The shell command may be returning before the window is first displayed, which could explain why hWnd=0. Have you tried the link posted by MartinLiss?
Insomnia is just a byproduct of, "It can't be done"
I did try the code from the website suggested by MartinLiss but I got it wrapped around my head
I copied the private decs and put them at the top of the form. I then copied the fuction ShellandAll and then I done this:
Private Sub CmdSend_Click()
Dim retval
retval = Shell("C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", vbNormalFocus)
'********************************
SendKeys "abc", 1
SendKeys "{TAB}", 1
SendKeys "Your mesage here", 1
end sub
Where the line of stars are is where I think I needed to call the ShellandAll function to perform the wait but no matter what I tried it wouldn't work
I'm starting to pull my hair out because I keep getting both the different ways wrong and I can't seem to find the way forward
I did try the code from the website suggested by MartinLiss but I got it wrapped around my head
I copied the private decs and put them at the top of the form. I then copied the fuction ShellandAll and then I done this:
Private Sub CmdSend_Click()
Dim retval
retval = Shell("C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", vbNormalFocus)
'********************************
SendKeys "abc", 1
SendKeys "{TAB}", 1
SendKeys "Your mesage here", 1
end sub
Where the line of stars are is where I think I needed to call the ShellandAll function to perform the wait but no matter what I tried it wouldn't work
I'm starting to pull my hair out because I keep getting both the different ways wrong and I can't seem to find the way forward
i tested your code with a program that i have(priginal text editor program), and works fine. and only executes 1 time.
other thing i need to know: when you execute the program who win the focus, is the textbox?
The fuction to cause the delay isn't being used because it isn't being called at any point so I'm still getting two forms up instead of one and the message is being written across both
when you did the message i was editing the message.... see the message again..
stay well
Last edited by joaquim; Mar 22nd, 2008 at 02:15 PM.
I've took out the retval = and the brackets and I'm still getting the same problem (calling two forms and message splitting along the two)
if you click again in button the program will be open again....
for you block these problem you can use a timer(intead a wait api function). the timer is for catch(use the findwindow api function) if the nokia program is open or not and if it's you can disable the form(enable form propertie to false).
Last edited by joaquim; Mar 22nd, 2008 at 02:30 PM.
Going back to the link MartinLiss suggested this is my full code on a single form with just one button called CmdSend:
Code:
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400
Public Function ShellandWait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0) As Boolean
Dim lInst As Long
Dim lStart As Long
Dim lTimeToQuit As Long
Dim sExeName As String
Dim lProcessId As Long
Dim lExitCode As Long
Dim bPastMidnight As Boolean
On Error GoTo ErrorHandler
lStart = CLng(Timer)
sExeName = ExeFullPath
'Deal with timeout being reset at Midnight
If TimeOutValue > 0 Then
If lStart + TimeOutValue < 86400 Then
lTimeToQuit = lStart + TimeOutValue
Else
lTimeToQuit = (lStart - 86400) + TimeOutValue
bPastMidnight = True
End If
End If
lInst = Shell(sExeName, vbMinimizedNoFocus)
lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)
Do
Call GetExitCodeProcess(lProcessId, lExitCode)
DoEvents
If TimeOutValue And Timer > lTimeToQuit Then
If bPastMidnight Then
If Timer < lStart Then Exit Do
Else
Exit Do
End If
End If
Loop While lExitCode = STATUS_PENDING
ShellandWait = True
ErrorHandler:
ShellandWait = False
Exit Function
End Function
Private Sub CmdSend_Click()
Shell "C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", vbNormalFocus
SendKeys "abc", 1
SendKeys "{TAB}", 1
SendKeys "Your mesage here", 1
End Sub
Is the ShellandAll function being called as standard because its public or do I need to somehow/somewhere call it in between loading the form and typing the text for the delay to happen?
yes exactly the same exe file but just two of them just like bringing up two calculators for example and putting half the numbers in one calculator and the other half of the numbers in to the other
yes exactly the same exe file but just two of them just like bringing up two calculators for example and putting half the numbers in one calculator and the other half of the numbers in to the other
i'm sorry but try these code:
Code:
Option Explicit
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Command1_Click()
Shell "C:\Documents and Settings\Joaquim\Os meus documentos\Visual Basic 6.0\Cambalinho\Cambalinho.exe", vbNormalFocus
'********************************
SendKeys "abc", 1
SendKeys "{TAB}", 1
SendKeys "Your mesage here", 1
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
If StatusProgram("Cambalinho") = False Then
Command1.Enabled = True
Timer1.Enabled = False
Else
Command1.Enabled = False
End If
End Sub
Private Function StatusProgram(strProgram As String) As Boolean
Dim s As Long
s = FindWindow(vbNullString, strProgram)
If s <> 0 Then
StatusProgram = False
Else
StatusProgram = True
End If
End Function
i edit again these message, because i found some errors... but now is ok..
tell me something
Last edited by joaquim; Mar 22nd, 2008 at 02:58 PM.
I'm still getting two forms - The hWnd function doesn't work because when I get through it says it equals to zero even when it shoudn't be
i'm sorry... don't forget change the strings for your program...
yes i know, but see again the last message... i put more and diferent code, try test it...
I'm thinking that the reason it can't find the hwnd is because the name in the brackets is wrong i.e.
hwnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")
or
hwnd = FindWindow(vbNullString, "C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe")
I there any way of finding out what it should be? I've tried both and nether work - Just in case its something completly different
no.... the findwindow just see if one form with the some caption is visible or open...
my new code is working, but sometimes is ignored because the form minize automatic... if you execute the program outside of vb, the problem is resolved(at least i think)
Actually scrap that comment the only reason it said the nokia bit was because it was calling the name from the other function
I'm still messing with the code but I can't seem to get it working YET
EDIT:
I don't think it likes the hWnd at all no matter what I seem to try so I think I might be better going and trying to work out the code from post 16 and somehow call the function that causes the delay
What do you think?
Last edited by bubblegum_girl; Mar 22nd, 2008 at 03:51 PM.
Actually scrap that comment the only reason it said the nokia bit was because it was calling the name from the other function
I'm still messing with the code but I can't seem to get it working YET
EDIT:
I don't think it likes the hWnd at all no matter what I seem to try so I think I might be better going and trying to work out the code from post 16 and somehow call the function that causes the delay
What do you think?
heres the project that i did for you....
test the exe... then tell me
PS: (in portuguese)"nova pasta" is in inglish "new folder"
I downloaded it and ran it and it brought up the text messaging editor form twice
Does it do that on your pc or is it just me?
well i had a car accident today, but i'm ok too ... in my pc open only one time, and do the work ok(i test with other program, but i change the code for you)....
now i don't know what hapen... i'm sorry...
omg you are going to laugh but after all that I have got it to work using my first bit of code and the line: Sleep 5000
It then waits for the form fully loading before writing and for some reason the text being writen before hand was interfering with the code and making it load another
It doesn't do that now - I can't believe something so simple like setting a sleep would work after all the code we tried
Thank you sooooooooooooo much for all your help
p.s Your as bad as me I'm walking with a limp - I fell over on Friday last week and sprained my ankle - Thanks again for all your help