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

ShellExecute won't open Manually Associated files

Status
Not open for further replies.

BruceBussell

Programmer
Jul 14, 2003
49
US

The following ShellExecute code won't open Manually Associated files, but it will open files like PDF, DOC, and XLS that have Installed File Associations.

If I manually Associate a text file like SAMPLE.D12 with an editor like TEXTPAD 4.5 (using the Always Open With option), then when I run this code it won't load this Manually Associated file.

How does one get ShellExecute or an equivalent way to Open Manually Associated files that were associated using the Windows Always Open This File With option.

Bruce Bussell

---------------------------------------------------------
VB6 Sample 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 Const SW_SHOWNORMAL = 1

Private mlngHandle As Long

Private Sub Command1_Click()

Dim plngHandle As Long

Call ShellExecute(plngHandle, "open", "c:\test.pdf", 0, 0, SW_SHOWNORMAL)

End Sub
---------------------------------------------------------


 
I have just tested this on windows XP.

Created a new file called grim.aaa, double clicked on it and selected Notepad and always open with this application.

Then using the exact code above, only changing file name, opened the file using VB.

Are you getting any error messages? If not I would assume that the file type has not been registered correctly.

Open My computer and click on Tools, Then Folder Options.

Click on the FileTypes tab and scroll to the file type that you are having problems with.

For the shellexecute code to work the "open" part of your code has to have a corresponding entry in the File Type dialog.

If not then this needs to be created.

If there is an open entry then check the details of this and make sure it is pointing to the correct executable.


Greg Palmer
Free Software for Adminstrators
 
Greg Palmer,

Thanks for the tip. It seems to work pretty well in VB6 for ShellExecute. However see the steps I had to take below to get the File Association to work OK.
-------------------------------------------------------

I am using Win2k SP3, and I found that simply double clicking on my SAMPLE.D12 file and selecting Always Open With This Application did not necessarily create an entry in the Tools, Folder Options, FileTypes tab list.

I had to Click on NEW FileType and enter D12 or DAT for example, and then click on the Advanced Tab and select Textpad as my Associated FileType from the dropdown list. Then my VB6 ShellExecute would work OK on my Win2k SP3 system.

NOTE: I found that the Delete Button is greyed out and I can't Delete these New D!@ and DAT FileTypes that I created manually. I wonder why this is.

Also I had to Edit the TXT FileType and change the Associated FileType for its "Open" option from Notepad to Textpad, to get ShellExecute to use Textpad to open TXT files with VB6 Shell Execute.

So although I had to do some extra work, I understand how to setup FileTypes to have a File Association that will work with VB6 ShellExecute.

Thanks for the Help,

Bruce Bussell

----------------------------------------------------------

Ref your note about getting any error messages. Attached is a sample from my VB6 Help of Shell Execute code that I ended up using most of, that has error messages.

I was getting the following error when a FileType was not set up correctly, as I described above:

Case SE_ERR_NOASSOC
msg = "No association for file extension"

----------------------------------------------------------

Start a New project in Visual Basic. Form1 is created by default.

Add the following code to the General Declarations section of Form1:


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 Form_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 = "File not found"
Case SE_ERR_PNF
msg = "Path not found"
Case SE_ERR_ACCESSDENIED
msg = "Access denied"
Case SE_ERR_OOM
msg = "Out of memory"
Case SE_ERR_DLLNOTFOUND
msg = "DLL not found"
Case SE_ERR_SHARE
msg = "A sharing violation occurred"
Case SE_ERR_ASSOCINCOMPLETE
msg = "Incomplete or invalid file association"
Case SE_ERR_DDETIMEOUT
msg = "DDE Time out"
Case SE_ERR_DDEFAIL
msg = "DDE transaction failed"
Case SE_ERR_DDEBUSY
msg = "DDE busy"
Case SE_ERR_NOASSOC
msg = "No association for file extension"
Case ERROR_BAD_FORMAT
msg = "Invalid EXE file or error in EXE image"
Case Else
msg = "Unknown error"
End Select
MsgBox msg
End If
End Sub

Run the project and click the form.

----------------------------------------------------------

 
From Bruce Bussell,

Corrected a typo in my previous reply:

NOTE: I found that the Delete Button is greyed out and I can't Delete these New D12 and DAT FileTypes that I created manually. I wonder why this is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top