how to run shell command in VB?

hi all,
I want to run shell command from my application. My exe(in my case, winword.exe) is in Program Files Folder
so i tried the following code,
But both of my exe and html files are in Program Files folder which has a empty space in their name.


dim llShellRet as long
dim lsPath as string
lsPath = "C:\Program Files\Microsoft Office\Office\Winword.exe C:\Program Files\Kbs9a\html\satbintro.htm"

llShellRet = Shell(lsPath & "", vbMaximizedFocus)

So actually what happens is Microsoft word is actually open but it's looking for C:\Program.doc and later looking for Files\Kbs9a\html\satbintro.htm file and doesn't open the correct file and opens a new file.

How to avoid this situation.
I can't ask the user not to leave space in their name or not to install the application in Program files folder.
but how to overcome this problem?

Thanks in advance,
mskannan


mskannanAsked:
Who is Participating?
 
BarryTiceConnect With a Mentor Commented:
The, um, challenges with ShellExecute, as I understand them, involve the potential inconsistencies from platform to platform when using API calls. (Mind you, this is what I've heard, not what I've experienced. I'm self-taught at VB, so I could be entirely off my rocker on this.)

I ran into the same problem you did with long file names, mskannan, when using the Shell. Adding quotes will do what you need -- kind of.

The below will work _if_ the path to your target file has a space in it (which it does in this case, in the middle of "Program Files"). If your target path doesn't have a space in it (e.g., if the path were "C:\satbintro.htm"), this throws back an error. Therefore, if your target file is not fixed, you should do this conditionally, checking for the space with an Instr() function.

dim llShellRet as long
dim lsPath as string

lsPath = "C:\Program Files\Microsoft Office\Office\Winword.exe " & chr(34) & "C:\Program Files\Kbs9a\html\satbintro.htm" & chr(34)

llShellRet = Shell(lsPath, vbMaximizedFocus)

Hope this helps -- b.r.t.

0
 
GordonpCommented:
Try the ShellExecute API, I havent had any problems with spaces.

API Declare is

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

See

http://msdn.microsoft.com/library/psdk/shellcc/shell/Functions/ShellExecute.htm

for MSDN page on how to use it.

GordonP
0
 
caraf_gCommented:
Put the whole thing in double quotes:

lsPath = """C:\Program Files\Microsoft Office\Office\Winword.exe"" ""C:\Program Files\Kbs9a\html\satbintro.htm"""

Et voilá

PS - this is not the correct way to shell a Word application ;-)

Like GordonP says, ShellExecute is a better option.

Check out the link "Start the correct default action for any file" from
http://homepage.eircom.net/~carafa/VB/Pino.htm

G'luck

Pino
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Gotcha007Commented:
You can do that by another way.

Create a Class Module (Project Menu and choose add a class module) and name it "EventClassModule"
add this code :

Public WithEvents MyWordObject As Word.Application


Private Sub Class_Initialize()

   'Instantiate a new instance of Word and add
   'a new document
   Set MyWordObject = New Word.Application

End Sub


In you form add :

 Set X = New EventClassModule
X.MyObjectWord.Documents.Open "c:\MyWordDoc.doc"


For any help about the word object in vb, launch word and choose help about VB.

Gotcha
0
 
caraf_gCommented:
Gotcha, not a bad idea, but since it is just as valid as any of our suggestions it would be a bit more "politically correct" if you were to add your tuppence as a comment rather than an answer and allow mskannan to make up his/her own mind. Would you mind withdrawing your answer for the moment?
0
 
Gotcha007Commented:
You can do that by another way.

Create a Class Module (Project Menu and choose add a class module) and name it "EventClassModule"
add this code :

Public WithEvents MyWordObject As Word.Application


Private Sub Class_Initialize()

   'Instantiate a new instance of Word and add
   'a new document
   Set MyWordObject = New Word.Application

End Sub


In you form add :

 Set X = New EventClassModule
X.MyObjectWord.Documents.Open "c:\MyWordDoc.doc"


For any help about the word object in vb, launch word and choose help about VB.

Gotcha
ps : you should add a reference to word 8 object library.
It's better than shellexecute because you don't have to know the path of Microsoft Word and you have plentiful of fonctions to intercat with it.

0
 
GordonpCommented:
ShellExecute is what Windows Explorer uses when you double-click on a file, to open the application with that file.

Its a core API Call and doesnt suffer from the 'challenges' of some other API calls when switching between NT/95/98/2K

Gordon
0
 
scj1Commented:
With respect to long filenames (including spaces etc.) you should use a Windows function call to get the short version. The following code does this:

'WINDOWS API Function declarations
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

'WINDOWS API wrapper
Public Function GetShortName(ByVal sLongFileName As String) As String
    Dim lRetVal As Long, sShortPathName As String, iLen As Integer
    'Set up buffer area for API function call return
    sShortPathName = Space(255)
    iLen = Len(sShortPathName)

    'Call the function
    lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
    'Strip away unwanted characters.
    GetShortName = Left(sShortPathName, lRetVal)
End Function

Just cut and paste this code and pass in the full long filename into the function GetShortName and it will return the short name. Although most applications do have proper long time support I have found that most use a space to delimate between different files when opening and thus you would have to pass each filename without spaces or alternatively build a string using single and double quotes:

strPath = "'c:\my documents\my doc.doc'"

Double quotes do not work in the form of ""c:\my documents "", because you will raise a syntax error in the editor.

I hope this works. Incidently, use the getShortName function also if you are using Word application objects for the same reasons.
0
 
Erick37Commented:
See:

"Q139851 - 'Cannot Find...' Using Shell Command with Long File Names"

http://support.microsoft.com/support/kb/articles/Q139/8/51.asp
0
 
mskannanAuthor Commented:

Hi all,
Thanks for your response,
Initially, I tried the shellexecute API but it returned some error messages.

I tried BarryTice's suggestions, it worked fine for me.

So I have decided to give my points to BarryTice and my sincere thanks to all others who helped me solve this problem.

Thanks,
mskannan.
0
All Courses

From novice to tech pro — start learning today.