Solved

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

Posted on 2010-08-18
5
358 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Spend the day watching some CBT nuggets on the subject.
0
 
LVL 4

Author Closing Comment

by:Cristi_E
Comment Utility
Works great !
Thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

772 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

8 Experts available now in Live!

Get 1:1 Help Now