Shell function sometimes works, sometimes doesn't

H

HC Hamaker

I'm running the following code in Excel 2003 on Windows XP:

Dim calllms As String

calllms = "C:\LEICA\LMSASCII.EXE "
calllms = calllms & lblNewFilename.Caption + ".NA0"

taskid = Shell(calllms)

Most of the time this works, but sometimes I get runtime error 5, Invalid
procedure or function call, when the Shell function is executed. The variable
calllms evaluates to

"C:\LEICA\LMSASCII.EXE C:\DOCUME~1\CHAMAK~1\LOCALS~1 \Temp\tmpa0n.NA0"

with no spaces in the file name. If I copy this string and execute it from
the run command under Start, there are no problems whatsoever. I've tried
ShellExecute as well, as it is also inconsistent. Any ideas about what might
be the problem?
 
N

NickHK

"C:\LEICA\LMSASCII.EXE C:\DOCUME~1\CHAMAK~1\LOCALS~1
\Temp\tmpa0n.NA0"
Sure that's correct ? With spaces after "LOCALS~1".

Also, this file is in the temp folder. Are you sure it exists before the
call is made ?

Also, how are you getting this short name ?
With the API GetShortPathName ?

NickHK
 
H

HC Hamaker

There are no spaces in the file name, including the path. The line break is
created by this forum text box.

I am sure the file exists. I create it myself. I have verified it both by
looking at Windows Explorer and by copying the value of the calllms string
during debugging, pressing the Start button, the Run..., pasting the string
in the text box in the window, and running it successfully.

I get the path for the temp directory using the statement

temppath = Environ$("TEMP")

Hope this gives you some clues. Thanks.
 
H

HC Hamaker

Upon further research, I believe the underlying cause is that the application
I'm trying to start is 16-bit program, and the Shell function will choke on
that (even though it usually doesn't). The documentation says the workaround
is to use a 32-bit application to call 16-bit application, and use the Shell
function to start the 32-bit application. Does anyone have a suggestion on
how to do that? For example, does anyone know how to call the Start>Run... on
the task bar from VBA?
 
H

HC Hamaker

After some research, I've found that using the CreateProcess() API seems to
solve the problem. Unlike ShellExecute(), it doesn't care whether the
application is 32- or 16-bit. The documentation of it notes that Windows 95
and NT have to be treated differently; for my XP application, the latter was
the proper choice.

Thanks to you folks for your interest in my problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top