1

I have a vbscript that opens an excel workbook and runs a particular macro inside at a particular time on a particular day. Here it is

Option Explicit

Dim xlApp, xlBook

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

While not (Month(Now()) = 11 and Day(Now()) = 17 and Hour(Now()) = 9 and Minute(Now()) = 1)
Wend

Set xlBook = xlApp.Workbooks.Open("C:\Temp\Property by Peril.xlsm")
xlApp.Application.Run "'Property by Peril.xlsm'!Main"

Set xlBook = Nothing
Set xlApp = Nothing

WScript.Echo "Finished."
WScript.Quit

It runs fine up to and including the opening of the workbook and the running of the macro. However, immediately after the macro runs, I get an error

Microsoft VBScript runtim error: Unknown runtime error   (on line 16)

and the rest of the script does not get executed.

Any thoughts/help?

8
  • Have you tried running the macro from Excel directly? It may not be your VBScript failing but the macro itself..? Commented Nov 17, 2014 at 15:45
  • Yes, @vba4all , good thinking. But I did run it directly and it works just fine on its own. Commented Nov 17, 2014 at 15:45
  • have you tried removing the workbook name? it's not necessary just leave the macro name (.Run "Main")? Also which one is line 16? Commented Nov 17, 2014 at 15:53
  • I just tried your suggestion, @vba4all , but with the same result. It opens the workbook and runs the macro, but then stops there. Line 16 is the .Run line, which is odd because that line runs fine. It's just the ones after that don't run and the error is triggered. Commented Nov 17, 2014 at 15:59
  • hm.. maybe you're missing the xlApp.close line or something that will close the file before setting references to Nothing? can you share the code from Main? Commented Nov 17, 2014 at 16:07

2 Answers 2

2

Try removing the statement to close the workbook from the macro and adding it to the VBScript. Also, don't forget to quit the application object, otherwise your hidden Excel instance will keep lingering in memory, because it's not automatically discarded when the script terminates.

...
xlApp.Application.Run "'Property by Peril.xlsm'!Main"

xlBook.Close False
xlApp.Quit

Set xlBook = Nothing
Set xlApp = Nothing
...

The parameter False will prevent changes to the workbook from being saved. Change it to True if you want changes to be automatically saved.

Sign up to request clarification or add additional context in comments.

Comments

1

The following code ran successfully on my system.

Option Explicit

Dim xlApp, xlBook

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

Set xlBook = xlApp.Workbooks.Open("C:\Temp\Property by Peril.xlsm")
xlApp.Application.Run "Main"

xlBook.Close False
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing

WScript.Echo "Finished."
WScript.Quit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.