Solved

Shell, ShellExecute, CreateProcess..blah blah blah

Posted on 2000-04-19
24
544 Views
Last Modified: 2010-05-18
I have tried using

shell
shellexecute
createprocess

to run an shortcut which points to a folder.  If I have testdir.lnk that poins to c:\testdir none of these calls will open the directory.  how can i open the directory a shortcut poins to.  Cant alter the shortcut.  The assumption is that the user is stupid and just clicks on pretty pictures.
0
Comment
Question by:groone
  • 9
  • 5
  • 5
  • +4
24 Comments
 
LVL 5

Expert Comment

by:KDivad
ID: 2733108
Simplest way:

x = Shell("Start " & LNKFileName)
0
 
LVL 3

Author Comment

by:groone
ID: 2733163
Sorry KDivad, shell doesnt work that way.  Thanks for the input though
0
 
LVL 3

Author Comment

by:groone
ID: 2733175
Let me rephrase that...it works that way, BUT it produces an error to open the directory folder and it does not support long names.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Expert Comment

by:Ruchi
ID: 2733291
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2733360
In support of KiDavid's comment.. it will support long file names if you just add a double quote before and after the LNKFileName. Eg.

Dim strFileNameLink as String
strFileNameLink = "c:\windows\desktop\Microsoft Word.lnk"

Shell("Start " & """" & strFileNameLink & """")

Where """" = 4 Double quotes in a row.

With that.. his solution works very nicely.. <smile>.



0
 
LVL 5

Expert Comment

by:KDivad
ID: 2733636
wsh2, Thanks for the defense.
0
 

Expert Comment

by:RuslanM
ID: 2733687
Just use :
Shell Explorer.exe & " " & DirectoryName

It should work.
0
 
LVL 3

Author Comment

by:groone
ID: 2734385
Shell("Start " & """" & strFileNameLink & """")

Works if the directory names are not long

If I had

strFileNameLink = "C:\my long named directory\and its long named subdirectory\shortcut to program.lnk"

Shell would not work, no matter what you did, AND with short names it still produces a dos error about incorrect parameter settings

0
 

Expert Comment

by:RuslanM
ID: 2734429
Try this :
Shell "Explorer.exe " " & LinkFileName
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2734976
RuslanM:
I think you mean the Directory/FileName contained in the shortcut, rather than the shorcut itself.. <smile>.

Shell "Explorer.exe """ & FileName & """"
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2737043
Here's what I did:

1. Created a shortcut to the folder "C:\Program Files\Accessories" and named it "Access.lnk"
2. Put it in the folder "C:\Program Files"
3. Shell "Start " & Chr$(34) & "C:\Program Files\Access.lnk" & Chr$(34)

It worked fine.
0
 
LVL 3

Author Comment

by:groone
ID: 2737390
Yes it opens the directory for browsing, BUT it creates a dos incorrect parameter error.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2738572
That's odd, because it doesn't on my machine.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2738668
KDivad.. your code runs perfectly.. <smile>. But I just saw something in groone's comment.. that could cause the error.. DOS only supports Directory pathnames up to 68 characters long.. <sigh>.. and groone loaded an epic.. <sheesh>.

Its still ok though.. we can still get around it with code.. <smile>. Using Groone's example:

Dim strLinkPath as String
strLinkPath = "C:\my long named directory\and its long named subdirectory\"

Dim strLinkFile as String
strLinkFile = "shortcut to program.lnk"

' Change Directory
ChDir strLinkPath

' Shell directly to the Link
Shell("Start " & """" & strLinkFile & """")

' Restore Directory
ChDir App.Path



0
 
LVL 3

Author Comment

by:groone
ID: 2738707
hmm...interesting wsh2

I will try that as soon as I get home
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2738716
Well.. now that I have a little time.. let me show you another way to do what the questioner wants.. <smile>. In your project set a reference (VB Menu -> Project -> References) to the "Windows Scripting Host Object" (WSHOM.OCX).. change the wshShell.CreateShortcut parameter below to a legitimate Link.. and Voile.. you have a Link to Executable runtime.. with no 68 character limitation.. <smile>.

<----- Code Begin ----->

Dim wshShell As IWshShell_Class
Set wshShell = CreateObject("Wscript.Shell")

Dim wshShortCut As IWshShortcut_Class
Set wshShortCut = wshShell.CreateShortcut("c:\windows\desktop\winamp.lnk") ' <-- CHANGE

Shell (wshShortCut.TargetPath)

<----- Code End ----->
0
 
LVL 4

Expert Comment

by:dreamvb
ID: 2738868
Dim zFilename As String
 zFilename = "C:\MyFolder.lnk"
  Shell "start.exe " & zFilename, vbHide
 
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2739411
<<DOS only supports Directory pathnames up to 68 characters long>>

Are you sure? I switched to DOS and played with this folder: "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\40\BIN" (which is 75 chars long) and had no trouble. Besides, if that was the case, should the folder have opened at all? groone says "Yes it opens the directory for browsing, BUT it creates a dos incorrect parameter error".

Groone, I have some code to extract the file/folder from a .lnk file. You could do this and use: Shell "explorer.exe " & Foldername as was previously mentioned. I haven't posted (or even mentioned) it because it took me a good while to figure it out and (in my opinion) is worth more than 50 points. The code itself is relatively simple, it just took me a while to decipher enough of the .lnk to get it. Returns either short or long path as desired.
0
 
LVL 3

Author Comment

by:groone
ID: 2739481
I'm working on trying to get the command line from the lnk.  There is a api call that does it, just doing some reading to figure out how it works.  If I get it, perhaps we can share code.  I's funny, because if I use the command line of the lnk insead of the actual shortcut the folder opens just fine.

As of right now, I sill get the dos box stating an incorrect parameter setting.

This is one of the wierdest things I have ever seen.  I believe there is more to it then jus saying
shell path, vbNormalShow
(the T on my keyboard is going out)

Unforunately I don have many more points so i will have o just hope for the bes...thanks anyway.
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 90 total points
ID: 2739978
You don't need APIs.. as I indicated earlier.. use the "Windows Scripting Host" to get the Shortcut.. extract the executable name, and then run it.

--------------------------------------
In your project set a reference (VB Menu -> Project -> References) to the "Windows Scripting Host Object" (WSHOM.OCX)

<----- Code Begin ----->

Dim wshShell As IWshShell_Class
Set wshShell = CreateObject("Wscript.Shell")

Dim wshShortCut As IWshShortcut_Class
Set wshShortCut = wshShell.CreateShortcut("c:\windows\desktop\winamp.lnk")

Shell (wshShortCut.TargetPath)

<----- Code End ----->

It is 4 lines of code.. and then a Shell command to run it.

======================================
As to the Invalid Parameter message, I am curious as to how your COMSPEC environment variable reads.


 
0
 
LVL 3

Author Comment

by:groone
ID: 2740050
rofl..you sure did mention that.  how could I have over looked hat.
0
 
LVL 3

Author Comment

by:groone
ID: 2740069
Adjusted points from 50 to 90
0
 
LVL 3

Author Comment

by:groone
ID: 2740070
Wsh2, perfect thanks!
0
 
LVL 9

Expert Comment

by:Dalin
ID: 2769387
viewed
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

Title # Comments Views Activity
using Access 8 75
Access Object Property from VBA Module in Excel 2010 2 37
Help me. 3 60
Child Form in front 4 49
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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