Solved

ExecuteAndWait

Posted on 2006-07-06
11
905 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 300 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now