Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access Denied using ShellExecute(

Posted on 2008-06-23
9
Medium Priority
?
1,149 Views
Last Modified: 2013-12-25
When using ShellExecute with some LNK Files as error 5 ( Access Denied ) error is returned. ( see example below )

However you can run the line below by clicking on
Start-Run and pasting C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office\Microsoft Office Word 2003.lnk

Any help in getting this issue resolved will be appreciated.


Example:
FileName = "C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office\Microsoft Office Word 2003.lnk"

iRet = ShellExecute(GetDesktopWindow, "open", FileName, vbNullString, vbNullString, vbNormalFocus)
If lRet = 5 Then
    MsgBox "Access Denied" & vbCrLf & "Try pointing directely to the program EXE", , ""
End If
0
Comment
Question by:PhilChapmanJr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21849110
Is there a reason you don't want to use WshShell.run or WshShell.execute?
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21849128
Also,  think that if you are going to be running a command, then it's best to point directly to the winword.exe (as your code mentions) so that you are not dependent on shortcuts that may or may not exist.
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 21849488
What is the synax for using WshShell in vb6?

I'm getting a list of the program from
C:\Documents and Settings\All Users\Start Menu\Programs
The program is often referrenced to throught a link file like.
C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office\Microsoft Office Word 2003.lnk

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Jared Luker
ID: 21849525
Sorry... I thought you were doing it in VBScript.

I would point to the .exe file.  Typically located at

c:\program Files\microsoft office\OFFICE11\winword.EXE
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 21849886
Pointing to the exe works fine.  But I will have not way of knowing what programs the user has.  Using the LNK files in VB works fine for some programs.  and if you run the line form the
Start-Run  prompt
All the LNK files work.

What I need to do is find out why all the LNK filds do not work in VB and work out a fix.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 21852485
I can't say for certain why it's not working for you but you can try my example below it might fix the problem. Only other thing I can think of is permission access or a corrupted association of the .lnk file itself.


Option Explicit
 
Private Const COINIT_APARTMENTTHREADED  As Long = &H2
Private Const COINIT_DISABLE_OLE1DDE    As Long = &H4
    
Private Declare Function CoInitializeEx Lib "OLE32.DLL" ( _
    ByRef pvReserved As Long, _
    ByVal dwCoInit As Long) As Long
 
Private Declare Function ShellExecuteA Lib "shell32.dll" ( _
    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
 
Public Function ShellIt(ByVal strPath As String) As Long
    
    Call CoInitializeEx(0, COINIT_APARTMENTTHREADED Or COINIT_DISABLE_OLE1DDE)
    ShellIt = ShellExecuteA(0, "open", strPath, vbNullString, vbNullString, 1)
    
End Function
 
Private Sub Command1_Click()
    Debug.Print ShellIt("C:\Documents and Settings\Admin\Start Menu\Programs\Accessories\Tour Windows XP.lnk")
End Sub

Open in new window

0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 21854527
I tried the code above but ShellIt returned a status of 5 and the program did not start.
0
 
LVL 10

Accepted Solution

by:
3_S earned 2000 total points
ID: 21857766
try the following code.  You must pass the startpath to the shellexecute function to avoid the error 5.
Additional coding can be done to check if the program (link) to open exists
if dir(sFullPath) <> "" then
   'do shellexecute
else
  msgbox sFullPath & " does not exists."
endif
    Dim sPath As String
    Dim sfilename As String
    Dim sFullPath As String
    Dim iRet As Integer
    sFullPath = "C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office\Microsoft Office Word 2003.lnk"
    sfilename = Dir(sFullPath)
    sPath = Left(sFullPath, Len(sFullPath) - Len(sfilename))
    iRet = ShellExecute(Me.hwnd, vbNullString, sFullPath, vbNullString, sPath, SW_SHOWNORMAL)
    If iRet = 5 Then
        MsgBox "Access Denied" & vbCrLf & "Try pointing directely to the program EXE", , ""
    End If

Open in new window

0
 
LVL 2

Author Closing Comment

by:PhilChapmanJr
ID: 31469793
Fantastic Work,  I was about to give up on it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

650 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