+ Reply to Thread
Results 1 to 5 of 5

Windows 7: How to run an "Elevated" command line?

Hybrid View

shawnvw Windows 7: How to run an... 07-11-2014, 08:04 PM
LJMetzger Re: Windows 7: How to run an... 07-12-2014, 09:52 AM
shawnvw Re: Windows 7: How to run an... 07-14-2014, 01:25 PM
LJMetzger Re: Windows 7: How to run an... 07-14-2014, 03:58 PM
shawnvw Re: Windows 7: How to run an... 07-14-2014, 05:59 PM
  1. #1
    Registered User
    Join Date
    09-25-2008
    Location
    Winnetka, CA
    Posts
    83

    Windows 7: How to run an "Elevated" command line?

    In my VBA procedure, I need to run the app "Skitch" and use it to open a JPEG file. This is the command I've been using:

        ReturnValue = Shell("C:\Program Files (x86)\Evernote\Skitch\Skitch.exe " & """" & aPic & """", 1)
    ...where "aPic" is the path and filename.

    After some experimenting, I think I need to run the command as if it were in an Elevated Command window (in other words, run it "as Administrator"). How can I do that?

    Thanks.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Windows 7: How to run an "Elevated" command line?

    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
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    09-25-2008
    Location
    Winnetka, CA
    Posts
    83

    Re: Windows 7: How to run an "Elevated" command line?

    I tried this, based on a similar example I found:

    Const SW_SHOW = 1
    Const SW_SHOWMAXIMIZED = 3
    
    Public 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
    
    Sub RunYourProgram()
      Dim RetVal As Long
     ' On Error Resume Next
      RetVal = ShellExecute(0, "open", "C:\Program Files (x86)\Evernote\Skitch\Skitch.exe ", "C:\Users\Work\Pictures\ACOF logo.png", _
                            "C:\Users\Work\Pictures", SW_SHOWMAXIMIZED)
    
    ' This does open Skitch but it doesn't open the picture. That's what happened with Shell. I think its still not Elevated.
    
    
    
    End Sub

    However, it once again opens Skitch with a "new" image, not the one I specified, which suggests that it's not Elevating.

    Is there something missing from my code that would Elevate it?

    When the app is running, is there a way to confirm that it's running Elevated?

    Thanks again.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Windows 7: How to run an "Elevated" command line?

    The following worked for me:
    Sub RunYourProgramAsAdministrator2()
      
      ShellExecute 0, "runas", "C:\Windows\System32\Notepad.exe", "C:\Users\Owner\Documents\Lew\WindowsBatchFiles\ExcelAndVba\x.txt", "", SW_SHOWMAXIMIZED
    
    End Sub
    Vista asks me if I want to run this program as Administrator. The only way around the prompt that I know of on Vista is to turn UAC off and reboot the computer.

    The following is the Microsoft specs with 'runas' undocumented:
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    See also:
    http://ss64.com/vb/shellexecute.html

    The ss64 link states:
    The runas verb is undocumented but can be used to elevate permissions. When a script is run with elevated permissions several aspects of the user environment may change: The current directory, the current TEMP folder and any mapped drives will be disconnected.
    Lewis

  5. #5
    Registered User
    Join Date
    09-25-2008
    Location
    Winnetka, CA
    Posts
    83

    Re: Windows 7: How to run an "Elevated" command line?

    When I do that, it asks me for permission to run, so I know it's running Elevated. Thanks.

    Unfortuantley, having the picture open still seems to only work part of the time. Sometimes it opens a picture I viewed in a previous session! This means my problem is something else entirely.




    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Create a Command Button to "Save As" and "Close" an Excel Workbook
    By thedunna in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-26-2013, 05:38 PM
  2. Replies: 4
    Last Post: 02-03-2008, 05:11 PM
  3. How to change a Command Button caption from "Enable" to "Disable"?
    By Infinity in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2007, 12:14 AM
  4. [SOLVED] Running command line "aplication"
    By dim in forum Excel General
    Replies: 0
    Last Post: 10-14-2005, 06:05 AM
  5. Is there a command line "switch" To close Excel
    By Randy Wayne in forum Excel General
    Replies: 2
    Last Post: 08-22-2005, 05:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1