Solved

how to run shell command in VB?

Posted on 2000-04-17
10
376 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now