Results 1 to 9 of 9

Thread: Best way to run an .exe

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2000
    Posts
    16
    I've developed an .exe that is fired from excel and winds up displaying it's results in a word document. After the user presses a command button on a worksheet, my .exe runs via a "shell" statement. Is there a better (or other) way to run the .exe?

    An an answer would be very much appreciated.


  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Question

    you could try the "start" statement?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2000
    Posts
    16

    "start" statement?

    da_silvy suggested using "start" to run an .exe from an excel macro as an alternative to "shell". I couldn't locate "start" in any of the books I have nor at the microsoft vb site. Can any one elaborate on start?

    Thanks

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    sorry, i think that function might only be availiable in vb 5/6 i think shell will probably be the most effective way to execute an executable..

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Shell

    What is wrong with the Shell start? What are you trying to do other then start the .exe? You can pass command line arguments, or use API calls or use a Wait function to see when it is finished.

  6. #6
    Member
    Join Date
    Mar 2000
    Location
    Switzerland
    Posts
    53
    I think this way would be nicer:

    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 CommandLine As String
    Private ErrCode As Long

    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 Long, 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 Long, _
    lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    PROCESS_INFORMATION) As Long

    Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    hObject As Long) As Long

    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As
    Long, _
    lpExitCode As Long) As Long

    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&


    Public Function ExecCmd(cmdline$) As Long
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ReturnValue As Integer

    ' Initialize the STARTUPINFO structure:
    start.cb = Len(start)

    ' Start the shelled application:
    ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
    NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

    ' Wait for the shelled application to finish:
    Do
    ReturnValue = WaitForSingleObject(proc.hProcess, 0)
    DoEvents
    Loop Until ReturnValue <> 258

    ReturnValue = GetExitCodeProcess(proc.hProcess, ExecCmd)
    ReturnValue = CloseHandle(proc.hProcess)
    End Function



    Sub Start_Exe()
    CommandLine = "C:\Program Files\Mircosoft Office\Office\Winword.exe"
    ErrCode = ExecCmd(CommandLine)
    If ErrCode <> 0 Then
    Call WhatError
    Exit Sub
    End If
    End Sub

    Sub WhatError()
    'Here you can insert the error handling of the called program
    End Sub


    Greetz Scand

  7. #7
    Member
    Join Date
    Mar 2000
    Location
    Switzerland
    Posts
    53
    Why does this forum doesn't accept tabs?

    Greetz
    Scand

  8. #8
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Scand,

    You can use vB code on this web site (similar to html). click on the (vB Code) link and it will exaplain all.

    just to get you started you can use the tags. Just place any code them

    ['code']
    'code goes here.
    ['/code']

    Obviously you don't use the " ' " character, they are just there to stop the forum thinking i am placing code.
    Iain, thats with an i by the way!

  9. #9
    Member
    Join Date
    Mar 2000
    Location
    Switzerland
    Posts
    53
    Thanks, now all is clearly.

    Try:
    Code:
    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 TypePrivate Type PROCESS_INFORMATION	hProcess As Long	hThread As Long
    	dwProcessID As Long	dwThreadID As LongEnd TypePrivate CommandLine As String
    Private ErrCode As Long
    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 Long, 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 Long, _
    	lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    	PROCESS_INFORMATION) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    	hObject As Long) As Long
    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As
    Long, _	lpExitCode As Long) As LongPrivate Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&Public Function ExecCmd(cmdline$) As Long
    	Dim proc As PROCESS_INFORMATION	Dim start As STARTUPINFO
    	Dim ReturnValue As Integer	' Initialize the STARTUPINFO structure:
    	start.cb = Len(start)	' Start the shelled application:
    	ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
    		NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    	' Wait for the shelled application to finish:	Do
    		ReturnValue = WaitForSingleObject(proc.hProcess, 0)		DoEvents
    	Loop Until ReturnValue <> 258
    	ReturnValue = GetExitCodeProcess(proc.hProcess, ExecCmd)
    	ReturnValue = CloseHandle(proc.hProcess)End FunctionSub Start_Exe()
    	CommandLine = "C:\Program Files\Mircosoft Office\Office\Winword.exe"
    	ErrCode = ExecCmd(CommandLine)	If ErrCode <> 0 Then		Call WhatError		Exit Sub
    	End IfEnd SubSub WhatError()
    	'Here you can insert the error handling of the called programEnd Sub
    Greetz Scand

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width