Hi shawnvw,
I think you need 'ShellExecute'. I have run it with .bat files, but I have never tried it with .exe files directly. In Vista (32 bit and 64 bit), UAC asks for permission to run as administrator, after the command is invoked. See my attached file with code to follow, that deletes the print queue. You may be able to adapt the code to your needs.
Lewis
Option Explicit
'Reference for CONDITIONAL COMPILATION CODE: http://www.jkp-ads.com/articles/apideclarations.asp
'VBA7 / Win64 CONDITIONAL COMPILATION has not been tested.
#If VBA7 And Win64 Then
' 64 bit Excel
'NOTE: The following line is supposed to be RED in 32 bit Excel
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
#Else
' 32 bit Excel
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
#End If
Sub CreateClearPrintQueueDotBatFile()
'This creates a text '.bat' file that clears the Windows print queue
'When the file is executed it needs 'Administrator' privileges to execute (VISTA)
'
'NOTE: This only creates a 'new' file. It will NOT OVERWRITE and existing file
'
'
'The contents of the file are (without the leading 'single quotes':
'
'@echo off
'if NOT exist %systemroot%\System32\spool\printers\*.s?? goto END
'net stop spooler
'if exist %systemroot%\System32\spool\printers\*.shd del %systemroot%\System32\spool\printers\*.shd /Q
'if exist %systemroot%\System32\spool\printers\*.spl del %systemroot%\System32\spool\printers\*.spl /Q
'net start spooler
':END
'exit
Const sFile = "ClearPrintQueue.bat"
Dim sTextFile As String
Dim iFileNo As Integer
Dim sPath As String
Dim sPathAndFile As String
'Get the path
sPath = ThisWorkbook.Path & "\"
'Create the full path and file name
sPathAndFile = sPath & sFile
'Create the file only if the file does not exist
If (Dir(sPathAndFile) = "") Then
'Allocate a file 'handle'
iFileNo = FreeFile
'Set error handler to close the file 'handle'
On Error GoTo CLOSEFILE
'Open the file for writing
Open sPathAndFile For Output As #iFileNo
'Write to the file
Print #iFileNo, "@echo off"
Print #iFileNo, "if NOT exist %systemroot%\System32\spool\printers\*.s?? echo The print queue is is empty."
Print #iFileNo, "if NOT exist %systemroot%\System32\spool\printers\*.s?? goto END"
Print #iFileNo, "net stop spooler"
Print #iFileNo, "echo Deleting files from the print queue."
Print #iFileNo, "if exist %systemroot%\System32\spool\printers\*.shd del %systemroot%\System32\spool\printers\*.shd /Q"
Print #iFileNo, "if exist %systemroot%\System32\spool\printers\*.spl del %systemroot%\System32\spool\printers\*.spl /Q"
Print #iFileNo, "net start spooler"
Print #iFileNo, ":END"
Print #iFileNo, "exit"
'Close the file
CLOSEFILE:
Close #iFileNo
On Error GoTo 0
End If
End Sub
Sub SpawnClearPrintQueueDotBatFileAsAdministrator()
'This tests running a program using 'Administrator' privileges.
'If UAC is turned on, the user has to AGREEE to run as administrator.
Const sFile = "ClearPrintQueue.bat"
Dim sPath As String
Dim sPathAndFile As String
'Get the path
sPath = ThisWorkbook.Path & "\"
'Create the full path and file name
sPathAndFile = sPath & sFile
Call LjmRunAsAdministrator(sPathAndFile)
End Sub
Sub LjmRunAsAdministrator(sPathAndFileName As String)
'This will run a file as 'Administrator'
'
'NOTE: 'ShellExecute' must be declared as an Alias to library to "shell32.dll"
ShellExecute 0, "runas", sPathAndFileName, Command & "/admin", vbNullString, vbNormalFocus
End Sub
Bookmarks