0

I am trying to execute an Excel VBA macro with a VBScript so that I can run it with cscript in batch. When I run it in Excel it works fine, but when I call the VBScript I get a permission error saying that create object is the cause.

I have tried rewriting the VBScript to no avail and it only works in Excel.

But the one that seems to be the most promising is below:

Option Explicit

On Error Resume Next

Sub ExcelMacro() 
  Dim xlApp 
  Dim xlBook 

  Set xlApp = CreateObject("Excel.Application") 
  Set xlBook = xlApp.Workbooks.Open("C:\MyWorkbook.xls", 0, True) 
  xlApp.Run "MyMacro"
  xlApp.Quit 

  Set xlBook = Nothing 
  Set xlApp = Nothing 
End Sub

The problem is that when I run it in the batch file, it shows that Windows starts to run it but then I get an error saying

Microsoft VBS runtime error: Permission denied Create Object.

How can I fix this?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Koos
  • 117
  • 4
  • 15
  • 1
    Maybe your antivirus or a company security policy – Tim Williams Aug 07 '19 at 21:07
  • Are you running the script with elevated permission. UAC *(User Account Control)* can be the cause of this type of issue. – user692942 Aug 07 '19 at 21:30
  • 1
    Is your script or Excel file really in plain `C:\ ` or is it in a sub folder like `C:\MyFiles\ `? If it is in plain `C:\ ` move it into a folder, Windows doesn't like you to put files into plain `C:\ ` and actually it is a very bad practice to put files there. – Pᴇʜ Aug 08 '19 at 06:52
  • As @Pᴇʜ pointed out , the root folder _(C:\ most of the time)_ is an absolute _off limits_ area for any Windows user. Even users with administrative privileges need to acknowledge changes there via UAC. So make it a good habit to _not_ put anything in there, not even for testing/debugging purposes. – Hel O'Ween Aug 08 '19 at 08:55

1 Answers1

0

After adding path "e.g. C:\Program Files\Java\jdk-11.0.5\bin" as PATH in System environment setting and doing restart of system has solved my issue.