Solved

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

Posted on 2010-08-18
5
368 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

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
Error handling in asp.net site 5 34
VB.Net WebRequest Code from JSON curl 7 48
location of a form 2 24
How to limit User Input 2 27
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
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…

791 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