Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to run shell command in VB?

Posted on 2000-04-17
10
381 Views
Last Modified: 2008-01-09
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


0
Comment
Question by:mskannan
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 3

Expert Comment

by:Gordonp
ID: 2721799
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2721809
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
 
LVL 1

Expert Comment

by:Gotcha007
ID: 2721969
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 10

Expert Comment

by:caraf_g
ID: 2722029
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
 
LVL 1

Expert Comment

by:Gotcha007
ID: 2722108
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
 
LVL 7

Accepted Solution

by:
BarryTice earned 50 total points
ID: 2722800
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
 
LVL 3

Expert Comment

by:Gordonp
ID: 2722866
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
 
LVL 1

Expert Comment

by:scj1
ID: 2722919
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2723203
See:

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

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

Author Comment

by:mskannan
ID: 2725447

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

860 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