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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change File Association 1

Status
Not open for further replies.

webmigit

Programmer
Aug 3, 2001
2,027
US
Quick question...

I'm writing a tool for notepad, or more specifically making notepad a tool of my program.. I want to give people the option to make my program the default text editor.. But don't want to explain how to change it manually.. The old saying is true...

Give a man a fish and he'll eat for a day,
Teach a man to fish and he'll eat for a lifetime.

And my addition...

But remember, he's gonna come after you the first time he smacks himself with the pole.

Tony Hicks
 
Brian.. uh... maybe you can help.. I emailed the code author the same question but no response...

If app.path has a space in it... it stops at the space... IE

My program is in:

C:\Documents and Settings\All Users\.....\Notepadthing.exe

And it set the executable location to be:

C:\Documents.exe

Can you help please?

Thanks,
Tony Hicks
 
Strange. I just tested this on my system (Windows XP) and it seemed to work. A few questions:

- Is the program compiled?
- Which OS version are you using, and which version of VB? (5 or 6)
- Could you post the call you are making?

If you want to e-mail me directly, remove the 'C' from the front of my username, and add "@att.net"
 
Ah... I found out a few minutes ago.. the uncompiled version is the one that trips...

Anyway since you've helped so much on this can you tell me how I might "shell()" files... You can shell(program) and shell("Explorer " + file) but this opens directly in IE if it can.. and that's really annoying with text files.

Thanks,
Tony Hicks
 
So, I take it you want to display a text file to the screen? You've almost got it anyway. You can execute virtually any .EXE from the SHELL command, and pass it a parameter (assuming, of course, that the program understands command line parms).

shell "notepad " & "C:\TEMP\Myfile.txt",vbNormalFocus

This will start notepad asynchronously, leaving your program running in the background.
 
I know that... I guess I said it wrong.. I want to basically shell a file and let Windows decide how to open it.. Any ideas?
 
Once it is associated in the registry, Windows should be able to open it with the proper file; but you may have to reboot before the association actually takes effect.

You can use the FindExecutable API call to see what the association is for a particular filetype.
 
Hehe... this is almost funny...

One more time....

Suppose I want to open file1.txt...

I could shell("Notepad " + "file1.txt")

And I know how to shell for a bmp file.. and many other extensions...

But what if someone changes ".txt" default editor to another program, to be user friendly I want to open the file with that program... so now...

shell("file1.txt") is basically what I want except that doesn't work, shell only shells programs and programs + command lines.

Get me now?

Thanks,
Tony Hicks
 
Try ShellExecute to open any file with its default application. Search this forum for examples of usage.

rgds
Andy
 
dear webmigit
here is the code to open the associted application
hope it will help you
Regards
Nouman


Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Const SW_SHOWNORMAL = 1

Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&

Function StartDoc(DocName As String) As Long
Dim Scr_hDC As Long
Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
"", "C:\", SW_SHOWNORMAL)
End Function

Private Sub Command1_Click()
Dim r As Long, msg As String
r = StartDoc("C:\WINDOWS\ARCADE.BMP")
If r <= 32 Then
'There was an error
Select Case r
Case SE_ERR_FNF
msg = &quot;File not found&quot;
Case SE_ERR_PNF
msg = &quot;Path not found&quot;
Case SE_ERR_ACCESSDENIED
msg = &quot;Access denied&quot;
Case SE_ERR_OOM
msg = &quot;Out of memory&quot;
Case SE_ERR_DLLNOTFOUND
msg = &quot;DLL not found&quot;
Case SE_ERR_SHARE
msg = &quot;A sharing violation occurred&quot;
Case SE_ERR_ASSOCINCOMPLETE
msg = &quot;Incomplete or invalid file association&quot;
Case SE_ERR_DDETIMEOUT
msg = &quot;DDE Time out&quot;
Case SE_ERR_DDEFAIL
msg = &quot;DDE transaction failed&quot;
Case SE_ERR_DDEBUSY
msg = &quot;DDE busy&quot;
Case SE_ERR_NOASSOC
msg = &quot;No association for file extension&quot;
Case ERROR_BAD_FORMAT
msg = &quot;Invalid EXE file or error in EXE image&quot;
Case Else
msg = &quot;Unknown error&quot;
End Select
MsgBox msg
End If
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top