Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I simulate a Windows Explorer double-click on a file?

Status
Not open for further replies.

SirPaladin

Programmer
Jul 9, 1999
32
0
0
US
I would have thought this would be simple, but I haven't found an easy way to launch a file with it's associated application (like would happen if you double-clicked the file in Windows Explorer). Give the complete path to a file, how do you 'execute' it? Right now I know how to launch a Word or Excel file, but I need to be able to trigger a file of any type to open in whatever application is associated with it's extension (such as an .html file launching a browser, a .jpg launching whatever image software is currently associated, etc.).
 
Hi,

You can use the shell function and pass the double-clicked file as a command line argument to its associated application.

Dim RetVal
RetVal = Shell("C:\WINDOWS\CALC.EXE", 1) ' Run Calculator

Have a good one!
BK
 
Paladin,
You need to use the ShellExecute api call. Here are the declarations to include. Create a text file on your C:\ drive called test.txt and open a new project and add a standard module. Paste the declaration and constants below into the module and add a command button to the form, then paste the form code into the form. When you click the button notepad will open with the test file. I got this example from so you can go there for a more detailed explanation. This should be exactly what you need.

Code:
'**** STANDARD MODULE *******
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

Public Const SW_MAXIMIZE = 3
Public Const SW_MINIMIZE = 6
Public Const SW_RESTORE = 9
'**** END MODULE CODE ************

'**** FORM CODE ******** 
Private Sub Command1_Click()
    
retval = ShellExecute(Form1.hwnd, "open", "C:\test.txt", "-fast", "C:\", _
        SW_MAXIMIZE)
End Sub
Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top