Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ExecuteAndWait

Posted on 2006-07-06
11
Medium Priority
?
939 Views
Last Modified: 2008-01-09


I've been using this sub for quite a while and suddenly it just stopped working..
dunno what caused it, might be a faulty commandline I'm sending..
what I would like the sub to do is start a program called bmconv.exe which is located in c:\MyProg\Ext\ the commandline would be extract.txt, this file is located in WINTEMPFOLDER\prog\themes\
WINTEMPFOLDER beïng c:\..., the tempfolder of windows :)

so what do I use to (try to) make it work
Call ExecuteAndWait ("c:\MyProg\Ext\bmconv.exe", "WINTEMPFOLDER\prog\themes\extract.txt")

The code below is pasted in a module:
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
Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadID As Long
End Type
Global Const NORMAL_PRIORITY_CLASS = &H20&
Global Const INFINITE = -1&

Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
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 String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


the code below is the sub:
Public Sub ExecuteAndWait(cmdline$, Optional SDir$)
    Dim NameOfProc As PROCESS_INFORMATION
    Dim NameStart As STARTUPINFO
    Dim x As Long

    NameStart.cb = Len(NameStart)
    If SHOWMODE = 0 Then
        NameStart.dwFlags = 1
        NameStart.wShowWindow = 0
    End If
    x = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, SDir$, NameStart, NameOfProc)
    x = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
    x = CloseHandle(NameOfProc.hProcess)
End Sub


I hope someone can help me out here,
thanks in advance
0
Comment
Question by:TheWiep
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 9

Expert Comment

by:justchat_1
ID: 17053061
open the command prompt...
navigate to WINTEMPFOLDER\prog\themes\extract.txt
try running c:\MyProg\Ext\bmconv.exe

Are there any problems doing that?
0
 

Author Comment

by:TheWiep
ID: 17053428
not sure how you want me to execute bmconv without the parameters (extract.txt)
as you see I don't know that much about cmd :)
0
 
LVL 9

Expert Comment

by:justchat_1
ID: 17053463
Isnt sdir$ the directery that the program is started in, not the parameters?...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:TheWiep
ID: 17053564
oops sorry,
the original line has lots of strings which would make it harder to follow so I changed with another path
and I made a mistake.. sorry
it has to be like below.

Call ExecuteAndWait ("c:\MyProg\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt", "WINTEMPFOLDER\prog\themes\")
0
 
LVL 9

Expert Comment

by:justchat_1
ID: 17054196
Thats better...
try start->run->"c:\MyProg\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt"
Does that work?
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 17054673
does this code work?:



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 WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&

' Start the indicated program and wait for it
' to finish, hiding while we wait.
Private Sub ShellAndWait(ByVal program_name As String, ByVal window_style As VbAppWinStyle)
Dim process_id As Long
Dim process_handle As Long
    ' Start the program.
    On Error GoTo ShellError
    process_id = Shell(program_name, window_style)
    On Error GoTo 0
    ' Hide.
    Me.Visible = False
    DoEvents
    ' Wait for the program to finish.
    ' Get the process handle.
    process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    If process_handle <> 0 Then
        WaitForSingleObject process_handle, INFINITE
        CloseHandle process_handle
    End If
    ' Reappear.
    Me.Visible = True
    Exit Sub
ShellError:
    MsgBox "Error starting task " & _
        txtProgram.Text & vbCrLf & _
        Err.Description, vbOKOnly Or vbExclamation, _
        "Error"
End Sub
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 17056400
may be

Try this

Dim WshShell

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "Your full path file name", 6, true
Set WshShell = nothing
0
 

Author Comment

by:TheWiep
ID: 17059142
I tried with run as justchat_1 suggested and it didn't work

I then "figured out" that the problem was a space in the path
when I remove the space from the path it works flawless
so.. now all I need is a way to use ExecuteAndWait with spaces

this works
start->run->"c:\MyProg\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt"

this doesn't (as there is a space between My and Prog)
start->run->"c:\My Prog\Ext\bmconv.exe WINTEMPFOLDER\prog\themes\extract.txt"

thanks for the replies already
0
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 1200 total points
ID: 17059906
try this:

"""c:\My Prog\Ext\bmconv.exe"" ""WINTEMPFOLDER\prog\themes\extract.txt"""
(in vb)

in cmd use this:

"c:\My Prog\Ext\bmconv.exe" "WINTEMPFOLDER\prog\themes\extract.txt"
0
 

Author Comment

by:TheWiep
ID: 17060115
thanks a lot
that did the trick
I used Chr(34) as I find it easier on the eye :)

thanks all for the help
gonna play around with it and add the Chr(34) everywhere
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 17060152

thanks for the points, and happy coding

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question