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