ShellExecuteEx giving Access Denied VBA 7 64 Bit

Gurnur Singh 21 Reputation points
2022-12-11T11:40:56.713+00:00

Hi Team,

I have written code below and executing from vba 7 editior using excel macro option

Public Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Public Declare PtrSafe Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As _
SHELLEXECUTEINFO) As Long
Public Declare PtrSafe Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal _
dwMilliseconds As Long) As Long
Public Const INFINITE = &HFFFF
Public Const WAIT_TIMEOUT = &H102
Public Const SE_ERR_FNF = 2
Public Const SE_ERR_NOASSOC = 31
Sub Macro1()
Dim pRef As Long, hComLibr As Long, hObjInt As Long, dwCoInit As Long, oFileOpenDialog As Object
Dim sei As SHELLEXECUTEINFO ' structure used by the function
Dim retval As Long ' return value

' Load the information needed to open C:\Docs\readme.txt  
' into the structure.  
With sei  
    ' Size of the structure  
    .cbSize = Len(sei)  
    ' Use the optional hProcess element of the structure.  
    .fMask = SEE_MASK_NOCLOSEPROCESS  
    ' Handle to the window calling this function.  
    ' The action to perform: open the file.  
    .hwnd = 0  
    .lpVerb = "runas"  
    ' The file to open.  
    .lpFile = "www.google.com"  
    ' No additional parameters are needed here.  
    .lpParameters = "runasadmin"  
    ' The default directory -- not really necessary in this case.  
    ' Simply display the window.  
    .nShow = SW_SHOWNORMAL  
    ' The other elements of the structure are either not used  
    ' or will be set when the function returns.  
End With  
  
' Open the file using its associated program.  
retval = ShellExecuteEx(sei)  
Debug.Print Err.LastDllError  
  
If retval = 0 Then  
    ' The function failed, so report the error.  Err.LastDllError  
    ' could also be used instead, if you wish.  
    Debug.Print sei.hInstApp  
    Select Case sei.hInstApp  
    Case SE_ERR_FNF  
        Debug.Print "The file C:\Docs\readme.txt was not found."  
    Case SE_NOASSOC  
        Debug.Print "No program is associated with *.txt files."  
    Case Else  
        Debug.Print "An unexpected error occured."  
    End Select  
Else  
    ' Wait for the opened process to close before continuing.  Instead  
    ' of waiting once for a time of INFINITE, this example repeatedly checks to see if the  
    ' is still open.  This allows the DoEvents VB function to be called, preventing  
    ' our program from appearing to lock up while it waits.  
    Do  
        DoEvents  
        retval = WaitForSingleObject(sei.hProcess, 0)  
    Loop While retval = WAIT_TIMEOUT  
    Debug.Print "Notepad (or whatever program was opened) has just closed."  
End If  

End Sub

But it is giving access denied. Not able to understand whats going on

Regards,
Gurnur Singh

Office Visual Basic for Applications
Office Visual Basic for Applications
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Visual Basic for Applications: An implementation of Visual Basic that is built into Microsoft products.
1,506 questions
{count} votes

Accepted answer
  1. Viorel 122.1K Reputation points
    2022-12-11T16:40:41.47+00:00

    If you want to open a web page using the default browser, then you must adjust some of the definitions:

    Public Type SHELLEXECUTEINFO  
        cbSize As Long  
        fMask As Long  
        hwnd As Long  
        lpVerb As String  
        lpFile As String  
        lpParameters As String  
        lpDirectory As String  
        nShow As Long  
        hInstApp As LongPtr  
        lpIDList As LongPtr  
        lpClass As String  
        hkeyClass As LongPtr  
        dwHotKey As Long  
        hIcon As LongPtr  
        hProcess As LongPtr  
    End Type  
      
    . . .  
      
    Public Declare PtrSafe Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As LongPtr, ByVal dwMilliseconds As Long) As Long  
      
    . . .  
      
     With sei  
         .cbSize = LenB(sei)  
         .fMask = 0  
         .hwnd = 0  
         .lpVerb = "open"  
         .lpFile = "www.bing.com"  
         .lpParameters = ""  
         .nShow = SW_SHOWNORMAL  
     End With  
     retval = ShellExecuteEx(sei)  
      
    . . .  
    

    Show the new issues and describe the problem.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.