tags:

views:

333

answers:

1

I want to open a file on my VB6 application by pressing a command. The file is a filetype read by another program. I want it to open it and the program that reads the filetype should open as well because when it finds that filetype it opens automatically. Can anyone help me with this I'm kind of a newb at VB6 (just got it yesterday).

+4  A: 

I'm kind of lost on the details of your requirement, but in general to launch a file from within VB6 you can use ShellExecute and its related API functions.

I'm not sure where the best reference is for this, but I suppose you can start here.

Edit:
You're close. Change this:

Private Sub smb3_Click()    
    ShellExecute hWnd, "find", "C:\hi\my.file", vbNullString, vbNullString, SW_SHOWNORMAL    
End Sub

to this:

Private Sub smb3_Click()    
    ShellExecute hWnd, "open", "C:\hi\my.file", vbNullString, vbNullString, SW_SHOWNORMAL    
End Sub

Note what I changed:
The second argument to ShellExecute should be "open" to open the specified file using the program on your machine that's associated with the extension of the file you're trying to open (the function's third argument). Check the link I included.

Which leads to something else you need to check on your machine (and the machines you'll use your program on). The file extension .file should be associated with whatever program you want to launch with your program.

Edit (Sept 9)

OK, let's see if we get the simplist implementation of ShellExecute to work.

Create a new VB6 project (Standard EXE) and add to Form1 one button named smb1.

Go to the form's Code View and copy and paste this code (and only this code):

Option Explicit

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 Sub smb3_Click()
    Debug.Print ShellExecute(hWnd, "open", "C:\hi\my.txt", vbNullString, vbNullString, 1)
End Sub

Make sure there's a text file called my.txt in your "c:\hi" folder.

Run the program and click the button. It should open the text file with the default text file editor program you have configured on your machine (wuch as Notepad).

If it doesn't please tell me what error you get and on which line the error occurs. Also, check the Immediate Window. The Immediate Window I believe will be visible as soon as you start your program and will remain visible as long as your program is running. If your code reaches and runs past the line with ShellExecute, the Immediate Window will display the code the call to that function returned. This will tell us a lot about your problem.

If the code works, we can think about changing the program to work on the file you need it to work on - on "C:\hi\my.file".

But first what happens when you double click the file in Windows Explorer? If the file opens up then we know there's a program that your system associated with files having the .file extension. If the file does not open you need to associate a program with the .file extension. Check Windows help for more. Once we're set, in your test program change the code in your smb3 Click Event to:

Debug.Print ShellExecute(hWnd, "open", "C:\hi\my.file", vbNullString, vbNullString, 1)

Run this and click the program. The file should open. If not, tell me what error you get and where, and tell me what (if anything) is in the Immediate Window.

If the program does open then you have in the button's click event and your declaration of ShellExecute the code you need to open files programmatically. But there's still a change you must make and changes you should make.

The change you must make is in the button's click event. We'll remove the debugging code so you end up with this:

ShellExecute hWnd, "open", "C:\hi\my.file", vbNullString, vbNullString, 1

The changes you should make involve some basic error handling around the call to ShellExecute. ShellExecute returns a code when it runs. This code could indicate an error. Study the sample program you can download on the page I linked above. In it, the programmer writes the result of his call to a variable called result. If result is equal to or less than 32 this indicates an error. In the sample program the error is displayed in an error message but think about what you might want to do.

Jay Riggs
basically its just supposed to open a file when i press a buttonand thats not helping me, i get a "Sub or Function not defined" error
It's hard for me to say what the problem is without seeing some code. I suggest scrolling to the bottom of the page I linked and downloading the sample, which does work. Study it (especially the cmdExecute_Click event) and move over the parts you need to your project. You won't have to move much, so don't be discouraged by the number of lines in the sample. Don't forget the declarations on the top of the sample form, you'll need at least one for ShellExecute.
Jay Riggs
this is my code:Private Sub smb3_Click()ShellExecute hWnd, "find", "C:\hi\my.file", _ vbNullString, vbNullString, SW_SHOWNORMALEnd Sub
See my edit.
Jay Riggs
ok i did that but i still get the error and it highlights the "Private Sub smb3_Click()" part
I have another edit.
Jay Riggs