创建以管理员权限运行.xlsm文件的.vbs脚本及解决GetObject无法控制管理员权限Excel进程问题
Got it, let's fix this issue right away! The problem you're facing comes down to Windows UAC privilege isolation—your regular-privilege VBS script can't interact with an Excel process running with elevated admin rights. Instead of trying to attach to the elevated Excel instance after launching it, we can simplify this by opening the .xlsm file directly with admin privileges in a single step.
Modified VBS Code
Instead of launching Excel first and then trying to control it, pass the .xlsm file path directly as an argument to Excel in the ShellExecute call:
Set objShell = CreateObject("Shell.Application") ' Update the paths below to match your Excel installation and target .xlsm file objShell.ShellExecute _ "C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE", _ """C:\Your\Full\Path\To\TargetFile.xlsm""", _ "", _ "runas", _ 1
Key Details:
- The double quotes around the .xlsm path (
"""..."") are necessary to handle spaces in the file path—VBS requires escaping double quotes with another double quote. - The final parameter
1makes Excel open in a visible window; use0if you need it to run in the background. - This approach skips the need for
GetObjectentirely, because Excel will automatically load your .xlsm file when launched with admin rights.
Why Your Original Approach Failed
When you use runas to launch Excel, it runs in an elevated security session. Your regular-privilege VBS script can't cross this session boundary to access the Excel instance via GetObject—this is a security feature of UAC designed to prevent low-privilege processes from manipulating high-privilege ones. There's no reliable way to bypass this without disabling UAC (which we don't recommend for security reasons).
内容的提问来源于stack exchange,提问作者chi Moon

