1

I want to create a .vbs file that runs the .xlsm file with administrator privileges.

The method I thought of is

  1. Run excel file as an administrator
  2. Open .xlsm file from an Excel opened with administrator privileges

I opened .xlsx as an administrator, but...

Set objShell = CreateObject("Shell.Application") 
objShell.ShellExecute "C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE", "" , "", "runas", 0

I cannot control the .xlsx file that I opened as an administrator by GetObject(,Excel.application)

I respectfully ask for your help.

Hackoo
  • 18,337
  • 3
  • 40
  • 70
chi Moon
  • 11
  • 2
  • Does this answer your question? [How to have vbs launch a program as administrator?](https://stackoverflow.com/q/46267758/692942) – user692942 Jan 10 '21 at 13:03
  • Does this answer your question? [VBScript- Single line as administrator](https://stackoverflow.com/a/13060293/692942) – user692942 Jan 10 '21 at 13:04
  • I think is safe to say, with a little bit of searching this topic is well covered. – user692942 Jan 10 '21 at 13:06

1 Answers1

0

You can give a try with this vbs file :

Option Explicit
Call Run_as_Admin() ' We execute our script with admin rights !
Dim Ws,EXCEL_FILE_PATH
Set Ws = CreateObject("Wscript.Shell")
EXCEL_FILE_PATH = "C:\Path\file.xlsm" ' Just change this line into your path file
Ws.run EXCEL_FILE_PATH,1,True
'----------------------------------------
Sub Run_as_Admin()
If Not WScript.Arguments.Named.Exists("elevate") Then
   CreateObject("Shell.Application").ShellExecute DblQuote(WScript.FullName) _
   , DblQuote(WScript.ScriptFullName) & " /elevate", "", "runas", 1
    WScript.Quit
End If
End Sub
'--------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'-------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • it doesn't work. Error message says there is the problem with _ rows 6, 1st string "Ws.run EXCEL_FILE_PATH,1,True" – chi Moon Jan 13 '21 at 05:14
  • @chiMoon What's the value did you modify for this variable `EXCEL_FILE_PATH` – Hackoo Jan 13 '21 at 07:29
  • I want to open some xlsm file, but if I put my .xlms path it doesn't work. Also I tried the path ""C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"" this also doesn't work. – chi Moon Jan 13 '21 at 07:39
  • @chiMoon check if you missed or added an extra double quote in your path ??? for me it works 5/5 ! – Hackoo Jan 13 '21 at 07:48