Solved

Open file without specifying the executable program to use (VB6)

Posted on 2010-08-18
5
360 Views
Last Modified: 2013-11-07
Hi
Is it possible in Visual Basic Classic to open a file (let's say c:\test.txt) without specifying the executable program to use (notepad.exe). Instead specifying the executable program i want to rely on the windows default executable that is set up to open that kind of file.

Regards,
Cristi
0
Comment
Question by:Cristi_E
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 200 total points
ID: 33462192
use shellExecute:
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



ShellExecute 0, vbNullString, """"c:\test.txt"""", vbNullString, vbNullString, vbNormalFocus

Open in new window

0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 300 total points
ID: 33462203
Use the ShellExecute API function to execute a file, launch the Find utility, and perform default actions
http://www.vb-helper.com/howto_shellexecute.html


Example: ShellExecute hWnd, "open", "C:\whatever\test.html", _
    vbNullString, vbNullString, SW_SHOWNORMAL

For more information see link
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 300 total points
ID: 33462209
If you want to pass the program you want to use, you can also perform this through shellExecute

You can use ShellExecute to do this and also pass other options to the receiving application. View below

Source: http://www.daniweb.com/forums/thread63235.html

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 'Magical functionPublic Function OpenLocation(ByVal WhichFilePath As String, Optional sParams As String = "", Optional sStartIn As String = vbNullString, Optional lngOpenMode As Long = 1) As Long    OpenLocation = ShellExecute(0, "Open", WhichFilePath, sParams, sStartIn, lngOpenMode)End Function 'Your button clickPrivate Sub Command1_Click() If OpenLocation("notepad.exe", "c:\myfile.txt", "", 1) < 32 Then    'Failed to openElse    'OpenedEnd If End Sub'Put this at the top of your form or module



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



'Magical function

Public Function OpenLocation(ByVal WhichFilePath As String, Optional sParams As String = "", Optional sStartIn As String = vbNullString, Optional lngOpenMode As Long = 1) As Long

    OpenLocation = ShellExecute(0, "<strong class="highlight">Open</strong>", WhichFilePath, sParams, sStartIn, lngOpenMode)

End Function



'Your button click

Private Sub Command1_Click()



If OpenLocation("notepad.exe", "c:\myfile.txt", "", 1) < 32 Then

    'Failed to <strong class="highlight">open</strong>

Else

    'Opened

End If



End Sub

Open in new window

0
 

Expert Comment

by:ziameo
ID: 33462259
Spend the day watching some CBT nuggets on the subject.
0
 
LVL 4

Author Closing Comment

by:Cristi_E
ID: 33462786
Works great !
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now