Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Shellexecute fails with Excel


I'm trying to get Excel VBA to fire up the relevant program for an
registered file type. Shellexecute is mentioned in many places as th
solution for this and indeed it works fine.

Except...

that when I select an Excel file type it just hangs there.

Can anyone explain to me why?

I'm not really a coder, just someone that throws bits together an
experiments but I can see no obvious reason why this should fail so
am guessing that it has something to do with the way that Excel use
the API call.

... why don't I just open the file in Excel? Well, I wanted to keep th
code simple rather than have to detect that it was an Excel fil
selected and treat it differently. Also unless I understand why i
doesn't work I am concerned that it might happen with another file typ
and application.

Within the code the getdesktop window call is in there simply because
saw it in one other example and thought that it might make a differenc
(not really understanding when the parameters can be dummied out an
window handles aren't often ones that you can miss out) but it made n
difference.

Thanks.

Code used to achieve this below.
-----------------------------------
Public Const SW_SHOWNORMAL = 1

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


Private Declare Function apiShellExecute 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 Declare Function GetDesktopWindow Lib "user32" () As Long


Sub Fred()
'
' Keyboard Shortcut: Ctrl+r
'
Dim sfilename2 As String
Dim X As Integer
Dim lRet As Long
Dim Scr_hDC As Long

Scr_hDC = GetDesktopWindow()

sfilename = Application.GetOpenFilename("All files (*.*), *.*", , "Fin
required file")
' To Exit if Cancel was pressed
If sfilename = False Then Exit Sub

sfilename2 = sfilename

lRet = apiShellExecute(Scr_hDC, "Open", sfilename2, vbNullString
vbNullString, SW_SHOWNORMAL)

If lRet <= 32 Then
'There was an error
Select Case lRet
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 Su

--
rappl
-----------------------------------------------------------------------
rapple's Profile: http://www.excelforum.com/member.php...fo&userid=3574
View this thread: http://www.excelforum.com/showthread.php?threadid=55523

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing XML with XSL into Excel fails Madhukar Excel Discussion (Misc queries) 0 July 30th 09 02:12 PM
Script fails in Excel was okay before. Jonah Excel Programming 14 March 5th 06 09:23 PM
Excel fails to save ChrisH Excel Discussion (Misc queries) 3 January 6th 06 01:30 PM
excel fails to launch NFEI Excel Discussion (Misc queries) 3 October 31st 05 08:13 PM
Excel Fails when add-in is selected Ron Excel Worksheet Functions 0 March 14th 05 12:18 AM


All times are GMT +1. The time now is 04:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"