こんにちは。
アプリケーションが、管理者権限で起動済みか判別するAPIはあるのでしょうか。
(IsUserAnAdminは違いますし)
Accessなら柔軟にコマンドライン引数を渡せるので、現行の判定方法で構いませんが、
Excelなので/eオプション等で判別せざるを得ないのがどうも腑に落ちません。
環境 Windows 7/Excel2010
Private Declare Function GetCommandLineW Lib "kernel32" () As Long
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
Sub ChangeAdmin()
Dim v As Variant
Dim f As Boolean
Dim lngLength As Long
Dim p As Long
p = GetCommandLineW()
lngLength = lstrlenW(p)
Buf = String$(lngLength, 0)
MoveMemory ByVal StrPtr(Buf), ByVal p, lngLength * 2
'Debug.Print Buf
v = Split(Buf)
Do
f = CBool(Len(v(1)))
If Not f Then Exit Do
f = CBool(StrComp(v(2), "/e", vbBinaryCompare))
If f Then Exit Do
'何か処理
Debug.Print "処理"
Exit Sub
Loop Until True
'Vista以降なら、管理者権限で再起動。
If Int(Right(Application.OperatingSystem, 4)) >= 6 Then
CreateObject("Shell.Application").ShellExecute "excel.exe", """" & ThisWorkbook.FullName & """ /e", "", "runas", 1
ThisWorkbook.Saved = True
Application.Quit
End If
End Sub
ツイート | ![]() |