Run Command Prompt as Administrator

1,220 views
Skip to first unread message

Michael Schin

unread,
Dec 4, 2013, 4:55:18 PM12/4/13
to exce...@googlegroups.com
Hi All,

Through Excel 2007 VBA I want to run the command prompt as administrator and run "control userpasswords2".

I've tried this pasted into it's own module:

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

Sub RunAsAdmin()
    Const SW_NORMAL As Long = 1

    ShellExecute hWndAccessApp(), _
                 "runas", _
                 "c:\windows\system32\cmd.exe", _
                 "/k control userpasswords2", _
                 "c:\windows\system32", SW_NORMAL
End Sub

I've also tried:

Shell "runas /user:administrator ""c:\windows\system32\cmd.exe /k control userpasswords2"""

The above does work as intended but isn't what I need.



What happens with the first snippet is "Compile error: Sub or Function not defined". With "hWndAccessApp" highlighted.

I recognize that "hWnd..." isn't defined, but I don't know how to define it. And I see that all the other declarations don't play a part in what I want to do - lpOperaion, lpFile etc.

I copied the snippets from: Access Blog - Running a Command as Administrator in VBA

Other info, system: win7 enterprise, 64bit.

Any help or direction pointing would be greatly appreciated.

Thank you.









Michael Schin

unread,
Dec 4, 2013, 5:20:44 PM12/4/13
to exce...@googlegroups.com
Just wanted to say that I figured it out.

What I was missing was the reference to MS Access.
Reply all
Reply to author
Forward
0 new messages