Solved

Better Shell Function

Posted on 1998-10-29
4
133 Views
Last Modified: 2010-04-30
I need to be able to shell and application, file shortcut etc. from my application. The RUN command on the START menu can do this, so I figure there must be a suitable API call. I have found lots of them, but they all do slightly different things. I need to be able to shell a file as if I had just double clicked on it. Can someone show me some code to do this???
0
Comment
Question by:bubbles
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:chrisbolt
ID: 1442477
0
 

Author Comment

by:bubbles
ID: 1442478
This is a good answer, but there are a couple of problems. I had actually already written code to do this. Anyway, this code cannot shell .lnk files (shortcuts). The other problem is that I need to be able to open a file using it's default action. E.g., if I make the filename a .wav file, I will want the function to Play it, not Open it, but I don't want my program to have to work out which action to perform. Maybe there is a constant to do this. I basically want something that will behave in the same way as the Run command on the Start menu.
0
 
LVL 2

Accepted Solution

by:
chrisbolt earned 120 total points
ID: 1442479
Option Explicit

Put this in the General: Declarations section of your main module:

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private 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

Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWDEFAULT As Long = 10

Then put this in the subroutine where you want to open a file with the default associated program using the default action, and working on .lnk files:

Dim RetVal As Long, hWndDesk As Long
hWndDesk = GetDesktopWindow()
RetVal = ShellExecute(hWndDesk, vbNullString, "C:\WHATEVER.TXT", vbNullString, "C:\", SW_SHOWNORMAL)

Obviously, replace C:\WHATEVER.TXT with the filename of the file you want to open/play/whatever.
0
 

Author Comment

by:bubbles
ID: 1442480
Sorry, but I actually got your previous answer to work. I had to play around with a few things. Anyway, sorry that I rejected your answer. Also, I have an identical question posted (something went wrong when I submited it) so if you answer that as well, you can have both lots of points. The question number is Q.10093842
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 73
Macro: print each sheet to pdf using ExportAsFixedFormat 11 48
passing parameter in sql procedure 9 61
Adding to a VBA? 6 69
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

786 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