Home All Groups Group Topic Archive Search About

Working with a .exe file

Author
6 Mar 2007 10:05 AM
Marco
Hi all,

I have a software ending with .exe that i would like to open, type
the
word 'select' on it, press 'enter' and make it run.


I used the following code :


wb = Shell("Y:\Programs\Sel.exe", 1)
    Application.Wait Now() + TimeValue("00:00:15")

    SendKeys "select"
    SendKeys "{ENTER}"

but the Sel.exe either copies the text "select" on my vbe editor
                            or shows the text "select" but it doesn't
recognize it.
When I open Sel.exe and I type "select" + "{ENTER}", without using vba
code,it works.

Any helps?


Thanks,
Marco

Author
6 Mar 2007 1:23 PM
Henning
Show quote Hide quote
"Marco" <marcomarr***@inwind.it> skrev i meddelandet
news:1173175509.231974.41820@64g2000cwx.googlegroups.com...
> Hi all,
>
> I have a software ending with .exe that i would like to open, type
> the
> word 'select' on it, press 'enter' and make it run.
>
>
> I used the following code :
>
>
> wb = Shell("Y:\Programs\Sel.exe", 1)
>     Application.Wait Now() + TimeValue("00:00:15")
>
>     SendKeys "select"
>     SendKeys "{ENTER}"
>
> but the Sel.exe either copies the text "select" on my vbe editor
>                             or shows the text "select" but it doesn't
> recognize it.
> When I open Sel.exe and I type "select" + "{ENTER}", without using vba
> code,it works.
>
> Any helps?
>
>
> Thanks,
> Marco
>
Not happy with the previous question on same topic?

/Henning
Author
6 Mar 2007 2:33 PM
Marco
Thanks /Henning but the problem still remains.
The code I used after many attempts is the following below.

In the case I let the vba code open the file and then it is me to
enter "select + "{ENTER}", the .exe file doesn't work.
In the case I open the file normally without vba code and i type in
"select + "{ENTER}" it works properly.

i'm really confused !!!??!!


Option Explicit
Private Type STARTUPINFO
   cb As Long
   lpReserved As String
   lpDesktop As String
   lpTitle As String
   dwX As Long
   dwY As Long
   dwXSize As Long
   dwYSize As Long
   dwXCountChars As Long
   dwYCountChars As Long
   dwFillAttribute As Long
   dwFlags As Long
   wShowWindow As Integer
   cbReserved2 As Integer
   lpReserved2 As Long
   hStdInput As Long
   hStdOutput As Long
   hStdError As Long
End Type


Private Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessID As Long
   dwThreadId As Long
End Type


Private Declare Function WaitForSingleObject _
    Lib "kernel32" (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA _
    Lib "kernel32" (ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, _
    ByVal lpCurrentDirectory As String, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long


Private Declare Function GetExitCodeProcess _
                          Lib "kernel32" (ByVal hProcess As Long, _
                                          lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
                                     (ByVal hObject As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = 32


Sub test()


   'experiment with the 1000 as you may need less or even 0
   ExecCmd "Y:\Programs\Sel.exe", 100000, True

End Sub



Function ExecCmd(sCmdLine As String, _
                 lmSecsWait As Long, _
                 Optional bShowWindow As Boolean) As Long


   'will start an external process and
   'wait till this process is finished
   'returns > -1 if successfull and -1 if not
   '-----------------------------------------
   Dim proc As PROCESS_INFORMATION
   Dim Start As STARTUPINFO
   Dim lReturn As Long


   'Initialize the STARTUPINFO structure:
   With Start
      .cb = Len(Start)
      .dwFlags = 1   'hexadecimal would be &H1
      If bShowWindow Then
         .wShowWindow = 1
      End If
   End With


   'Start the shelled application:
   lReturn = CreateProcessA(sCmdLine, _
                            vbNullString, _
                            0, _
                            0, _
                            1, _
                            NORMAL_PRIORITY_CLASS, _
                            0, _
                            vbNullString, _
                            Start, _
                            proc)


   'Wait for the shelled application to finish:
   lReturn = WaitForSingleObject(proc.hProcess, lmSecsWait)
   SendKeys "Select"
'   SendKeys "{ENTER}"
   GetExitCodeProcess proc.hProcess, lReturn
   CloseHandle proc.hThread
   CloseHandle proc.hProcess
   ExecCmd = lReturn


End Function
Author
6 Mar 2007 9:49 PM
Henning
I just tested this, and it works as expected.

ShellTest is just a form with a textbox and a commandbutton. I also compile
the same to ShellExec.exe, witch is run for the test. Press the button on
ShellExec, and ShellTest is started.

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
  If KeyCode = vbKeyReturn Then
    If Text1.Text = "1234" Then
      Text1.Text = "Yes it is"
    End If
  End If
End Sub

Private Sub Command1_Click()
  Dim a

  a = Shell("c:\ShellTest.exe", vbNormalFocus)

  AppActivate a

  SendKeys "1234"
  SendKeys "{ENTER}"

End Sub

And it changes the text to "Yes it is"

/Henning

Show quoteHide quote
"Marco" <marcomarr***@inwind.it> skrev i meddelandet
news:1173191581.186839.274280@q40g2000cwq.googlegroups.com...
> Thanks /Henning but the problem still remains.
> The code I used after many attempts is the following below.
>
> In the case I let the vba code open the file and then it is me to
> enter "select + "{ENTER}", the .exe file doesn't work.
> In the case I open the file normally without vba code and i type in
> "select + "{ENTER}" it works properly.
>
> i'm really confused !!!??!!
>
>
> Option Explicit
> Private Type STARTUPINFO
>    cb As Long
>    lpReserved As String
>    lpDesktop As String
>    lpTitle As String
>    dwX As Long
>    dwY As Long
>    dwXSize As Long
>    dwYSize As Long
>    dwXCountChars As Long
>    dwYCountChars As Long
>    dwFillAttribute As Long
>    dwFlags As Long
>    wShowWindow As Integer
>    cbReserved2 As Integer
>    lpReserved2 As Long
>    hStdInput As Long
>    hStdOutput As Long
>    hStdError As Long
> End Type
>
>
> Private Type PROCESS_INFORMATION
>    hProcess As Long
>    hThread As Long
>    dwProcessID As Long
>    dwThreadId As Long
> End Type
>
>
> Private Declare Function WaitForSingleObject _
>     Lib "kernel32" (ByVal hHandle As Long, _
>     ByVal dwMilliseconds As Long) As Long
>
> Private Declare Function CreateProcessA _
>     Lib "kernel32" (ByVal lpApplicationName As String, _
>     ByVal lpCommandLine As String, _
>     ByVal lpProcessAttributes As Long, _
>     ByVal lpThreadAttributes As Long, _
>     ByVal bInheritHandles As Long, _
>     ByVal dwCreationFlags As Long, _
>     ByVal lpEnvironment As Long, _
>     ByVal lpCurrentDirectory As String, _
>     lpStartupInfo As STARTUPINFO, _
>     lpProcessInformation As PROCESS_INFORMATION) As Long
>
>
> Private Declare Function GetExitCodeProcess _
>                           Lib "kernel32" (ByVal hProcess As Long, _
>                                           lpExitCode As Long) As Long
> Private Declare Function CloseHandle Lib "kernel32" _
>                                      (ByVal hObject As Long) As Long
> Private Const NORMAL_PRIORITY_CLASS = 32
>
>
> Sub test()
>
>
>    'experiment with the 1000 as you may need less or even 0
>    ExecCmd "Y:\Programs\Sel.exe", 100000, True
>
> End Sub
>
>
>
> Function ExecCmd(sCmdLine As String, _
>                  lmSecsWait As Long, _
>                  Optional bShowWindow As Boolean) As Long
>
>
>    'will start an external process and
>    'wait till this process is finished
>    'returns > -1 if successfull and -1 if not
>    '-----------------------------------------
>    Dim proc As PROCESS_INFORMATION
>    Dim Start As STARTUPINFO
>    Dim lReturn As Long
>
>
>    'Initialize the STARTUPINFO structure:
>    With Start
>       .cb = Len(Start)
>       .dwFlags = 1   'hexadecimal would be &H1
>       If bShowWindow Then
>          .wShowWindow = 1
>       End If
>    End With
>
>
>    'Start the shelled application:
>    lReturn = CreateProcessA(sCmdLine, _
>                             vbNullString, _
>                             0, _
>                             0, _
>                             1, _
>                             NORMAL_PRIORITY_CLASS, _
>                             0, _
>                             vbNullString, _
>                             Start, _
>                             proc)
>
>
>    'Wait for the shelled application to finish:
>    lReturn = WaitForSingleObject(proc.hProcess, lmSecsWait)
>    SendKeys "Select"
> '   SendKeys "{ENTER}"
>    GetExitCodeProcess proc.hProcess, lReturn
>    CloseHandle proc.hThread
>    CloseHandle proc.hProcess
>    ExecCmd = lReturn
>
>
> End Function
>
>
>
Author
7 Mar 2007 4:52 PM
Marco
Hi Henning,

I tried your code but on the line 'AppActivate a' i get the following
error:

Run-time error '5':
Invalid procedure call or argument
Author
7 Mar 2007 5:22 PM
Henning
"Marco" <marcomarr***@inwind.it> skrev i meddelandet
news:1173286323.874450.317480@p10g2000cwp.googlegroups.com...
>
> Hi Henning,
>
> I tried your code but on the line 'AppActivate a' i get the following
> error:
>
> Run-time error '5':
> Invalid procedure call or argument
>
Hmmm,
I created a macro in Excel Sheet1, aktivated with Ctrl-Shift-Z with
following VBA code in Sheet1 Code.

Sub ShellTest()

  Dim a

  a = Shell("c:\ShellTest.exe", vbNormalFocus)

  AppActivate a

  SendKeys "1234"
  SendKeys "{ENTER}"
End Sub

And it is still working as expected. So it works in VB6 and VBA for Excel.
What are you using?

/Henning
Author
8 Mar 2007 7:22 PM
Marco
Henning

I found the solution inputting the directory to which the exe file
referred to.


Thanks for your help,
Marco
Author
8 Mar 2007 3:47 PM
Jacquelin Hardy
I tried your code Henning, it works OK.

One question: Is it possible to terminate the main App when the user presses
the Command1 Button and to run only the "Shelltest.exe" instead of running
it as a sub thru the main App.?

Thank you

Jacquelin Hardy

"Henning" <computer_h***@coldmail.com> a écrit dans le message de news:
45ede35b$0$17842$57c3e***@news3.bahnhof.se...
Show quoteHide quote
>I just tested this, and it works as expected.
>
> ShellTest is just a form with a textbox and a commandbutton. I also
> compile
> the same to ShellExec.exe, witch is run for the test. Press the button on
> ShellExec, and ShellTest is started.
>
> Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
>  If KeyCode = vbKeyReturn Then
>    If Text1.Text = "1234" Then
>      Text1.Text = "Yes it is"
>    End If
>  End If
> End Sub
>
> Private Sub Command1_Click()
>  Dim a
>
>  a = Shell("c:\ShellTest.exe", vbNormalFocus)
>
>  AppActivate a
>
>  SendKeys "1234"
>  SendKeys "{ENTER}"
>
> End Sub
>
> And it changes the text to "Yes it is"
>
> /Henning
>
> "Marco" <marcomarr***@inwind.it> skrev i meddelandet
> news:1173191581.186839.274280@q40g2000cwq.googlegroups.com...
>> Thanks /Henning but the problem still remains.
>> The code I used after many attempts is the following below.
>>
>> In the case I let the vba code open the file and then it is me to
>> enter "select + "{ENTER}", the .exe file doesn't work.
>> In the case I open the file normally without vba code and i type in
>> "select + "{ENTER}" it works properly.
>>
>> i'm really confused !!!??!!
>>
>>
>> Option Explicit
>> Private Type STARTUPINFO
>>    cb As Long
>>    lpReserved As String
>>    lpDesktop As String
>>    lpTitle As String
>>    dwX As Long
>>    dwY As Long
>>    dwXSize As Long
>>    dwYSize As Long
>>    dwXCountChars As Long
>>    dwYCountChars As Long
>>    dwFillAttribute As Long
>>    dwFlags As Long
>>    wShowWindow As Integer
>>    cbReserved2 As Integer
>>    lpReserved2 As Long
>>    hStdInput As Long
>>    hStdOutput As Long
>>    hStdError As Long
>> End Type
>>
>>
>> Private Type PROCESS_INFORMATION
>>    hProcess As Long
>>    hThread As Long
>>    dwProcessID As Long
>>    dwThreadId As Long
>> End Type
>>
>>
>> Private Declare Function WaitForSingleObject _
>>     Lib "kernel32" (ByVal hHandle As Long, _
>>     ByVal dwMilliseconds As Long) As Long
>>
>> Private Declare Function CreateProcessA _
>>     Lib "kernel32" (ByVal lpApplicationName As String, _
>>     ByVal lpCommandLine As String, _
>>     ByVal lpProcessAttributes As Long, _
>>     ByVal lpThreadAttributes As Long, _
>>     ByVal bInheritHandles As Long, _
>>     ByVal dwCreationFlags As Long, _
>>     ByVal lpEnvironment As Long, _
>>     ByVal lpCurrentDirectory As String, _
>>     lpStartupInfo As STARTUPINFO, _
>>     lpProcessInformation As PROCESS_INFORMATION) As Long
>>
>>
>> Private Declare Function GetExitCodeProcess _
>>                           Lib "kernel32" (ByVal hProcess As Long, _
>>                                           lpExitCode As Long) As Long
>> Private Declare Function CloseHandle Lib "kernel32" _
>>                                      (ByVal hObject As Long) As Long
>> Private Const NORMAL_PRIORITY_CLASS = 32
>>
>>
>> Sub test()
>>
>>
>>    'experiment with the 1000 as you may need less or even 0
>>    ExecCmd "Y:\Programs\Sel.exe", 100000, True
>>
>> End Sub
>>
>>
>>
>> Function ExecCmd(sCmdLine As String, _
>>                  lmSecsWait As Long, _
>>                  Optional bShowWindow As Boolean) As Long
>>
>>
>>    'will start an external process and
>>    'wait till this process is finished
>>    'returns > -1 if successfull and -1 if not
>>    '-----------------------------------------
>>    Dim proc As PROCESS_INFORMATION
>>    Dim Start As STARTUPINFO
>>    Dim lReturn As Long
>>
>>
>>    'Initialize the STARTUPINFO structure:
>>    With Start
>>       .cb = Len(Start)
>>       .dwFlags = 1   'hexadecimal would be &H1
>>       If bShowWindow Then
>>          .wShowWindow = 1
>>       End If
>>    End With
>>
>>
>>    'Start the shelled application:
>>    lReturn = CreateProcessA(sCmdLine, _
>>                             vbNullString, _
>>                             0, _
>>                             0, _
>>                             1, _
>>                             NORMAL_PRIORITY_CLASS, _
>>                             0, _
>>                             vbNullString, _
>>                             Start, _
>>                             proc)
>>
>>
>>    'Wait for the shelled application to finish:
>>    lReturn = WaitForSingleObject(proc.hProcess, lmSecsWait)
>>    SendKeys "Select"
>> '   SendKeys "{ENTER}"
>>    GetExitCodeProcess proc.hProcess, lReturn
>>    CloseHandle proc.hThread
>>    CloseHandle proc.hProcess
>>    ExecCmd = lReturn
>>
>>
>> End Function
>>
>>
>>
>
>
Author
8 Mar 2007 7:24 PM
Henning
I guess (havn't tried), but change in ShellExec as follows is supposed to
work.

  SendKeys "1234"
  SendKeys "{ENTER}"

  DoEvents
  Unload Me

End Sub

/Henning

Show quoteHide quote
"Jacquelin Hardy" <jacha***@videotron.ca> skrev i meddelandet
news:epHzskZYHHA.4520@TK2MSFTNGP06.phx.gbl...
> I tried your code Henning, it works OK.
>
> One question: Is it possible to terminate the main App when the user
presses
> the Command1 Button and to run only the "Shelltest.exe" instead of running
> it as a sub thru the main App.?
>
> Thank you
>
> Jacquelin Hardy
>
> "Henning" <computer_h***@coldmail.com> a écrit dans le message de news:
> 45ede35b$0$17842$57c3e***@news3.bahnhof.se...
> >I just tested this, and it works as expected.
> >
> > ShellTest is just a form with a textbox and a commandbutton. I also
> > compile
> > the same to ShellExec.exe, witch is run for the test. Press the button
on
> > ShellExec, and ShellTest is started.
> >
> > Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
> >  If KeyCode = vbKeyReturn Then
> >    If Text1.Text = "1234" Then
> >      Text1.Text = "Yes it is"
> >    End If
> >  End If
> > End Sub
> >
> > Private Sub Command1_Click()
> >  Dim a
> >
> >  a = Shell("c:\ShellTest.exe", vbNormalFocus)
> >
> >  AppActivate a
> >
> >  SendKeys "1234"
> >  SendKeys "{ENTER}"
> >
> > End Sub
> >
> > And it changes the text to "Yes it is"
> >
> > /Henning
> >
> > "Marco" <marcomarr***@inwind.it> skrev i meddelandet
> > news:1173191581.186839.274280@q40g2000cwq.googlegroups.com...
> >> Thanks /Henning but the problem still remains.
> >> The code I used after many attempts is the following below.
> >>
> >> In the case I let the vba code open the file and then it is me to
> >> enter "select + "{ENTER}", the .exe file doesn't work.
> >> In the case I open the file normally without vba code and i type in
> >> "select + "{ENTER}" it works properly.
> >>
> >> i'm really confused !!!??!!
> >>
> >>
> >> Option Explicit
> >> Private Type STARTUPINFO
> >>    cb As Long
> >>    lpReserved As String
> >>    lpDesktop As String
> >>    lpTitle As String
> >>    dwX As Long
> >>    dwY As Long
> >>    dwXSize As Long
> >>    dwYSize As Long
> >>    dwXCountChars As Long
> >>    dwYCountChars As Long
> >>    dwFillAttribute As Long
> >>    dwFlags As Long
> >>    wShowWindow As Integer
> >>    cbReserved2 As Integer
> >>    lpReserved2 As Long
> >>    hStdInput As Long
> >>    hStdOutput As Long
> >>    hStdError As Long
> >> End Type
> >>
> >>
> >> Private Type PROCESS_INFORMATION
> >>    hProcess As Long
> >>    hThread As Long
> >>    dwProcessID As Long
> >>    dwThreadId As Long
> >> End Type
> >>
> >>
> >> Private Declare Function WaitForSingleObject _
> >>     Lib "kernel32" (ByVal hHandle As Long, _
> >>     ByVal dwMilliseconds As Long) As Long
> >>
> >> Private Declare Function CreateProcessA _
> >>     Lib "kernel32" (ByVal lpApplicationName As String, _
> >>     ByVal lpCommandLine As String, _
> >>     ByVal lpProcessAttributes As Long, _
> >>     ByVal lpThreadAttributes As Long, _
> >>     ByVal bInheritHandles As Long, _
> >>     ByVal dwCreationFlags As Long, _
> >>     ByVal lpEnvironment As Long, _
> >>     ByVal lpCurrentDirectory As String, _
> >>     lpStartupInfo As STARTUPINFO, _
> >>     lpProcessInformation As PROCESS_INFORMATION) As Long
> >>
> >>
> >> Private Declare Function GetExitCodeProcess _
> >>                           Lib "kernel32" (ByVal hProcess As Long, _
> >>                                           lpExitCode As Long) As Long
> >> Private Declare Function CloseHandle Lib "kernel32" _
> >>                                      (ByVal hObject As Long) As Long
> >> Private Const NORMAL_PRIORITY_CLASS = 32
> >>
> >>
> >> Sub test()
> >>
> >>
> >>    'experiment with the 1000 as you may need less or even 0
> >>    ExecCmd "Y:\Programs\Sel.exe", 100000, True
> >>
> >> End Sub
> >>
> >>
> >>
> >> Function ExecCmd(sCmdLine As String, _
> >>                  lmSecsWait As Long, _
> >>                  Optional bShowWindow As Boolean) As Long
> >>
> >>
> >>    'will start an external process and
> >>    'wait till this process is finished
> >>    'returns > -1 if successfull and -1 if not
> >>    '-----------------------------------------
> >>    Dim proc As PROCESS_INFORMATION
> >>    Dim Start As STARTUPINFO
> >>    Dim lReturn As Long
> >>
> >>
> >>    'Initialize the STARTUPINFO structure:
> >>    With Start
> >>       .cb = Len(Start)
> >>       .dwFlags = 1   'hexadecimal would be &H1
> >>       If bShowWindow Then
> >>          .wShowWindow = 1
> >>       End If
> >>    End With
> >>
> >>
> >>    'Start the shelled application:
> >>    lReturn = CreateProcessA(sCmdLine, _
> >>                             vbNullString, _
> >>                             0, _
> >>                             0, _
> >>                             1, _
> >>                             NORMAL_PRIORITY_CLASS, _
> >>                             0, _
> >>                             vbNullString, _
> >>                             Start, _
> >>                             proc)
> >>
> >>
> >>    'Wait for the shelled application to finish:
> >>    lReturn = WaitForSingleObject(proc.hProcess, lmSecsWait)
> >>    SendKeys "Select"
> >> '   SendKeys "{ENTER}"
> >>    GetExitCodeProcess proc.hProcess, lReturn
> >>    CloseHandle proc.hThread
> >>    CloseHandle proc.hProcess
> >>    ExecCmd = lReturn
> >>
> >>
> >> End Function
> >>
> >>
> >>
> >
> >
>
>