0
votes

in VBA, I have a global script control variable in JScript language (Javascript ES3). I have the var "ThisWorkbook" set as

var ThisWorkbook = GetObject('', 'Excel.Application').Workbooks('" + ThisWorkbook.Name + "')

Then I can close the workbook by saying

ThisWorkbook.Close(false)

Full VBA code for completion:

Sub Test

Dim ScriptC as Object
Set ScriptC = CreateObject("ScriptControl"): ScriptC.Language = "JScript"

ScriptC.Eval "var ThisWorkbook = GetObject('', 'Excel.Application').Workbooks('" + ThisWorkbook.Name + "')"
ScriptC.Eval "ThisWorkbook.Close(false)"

End Sub

However, this crashes my Excel and all open workbooks (Excel 2016) - what would be a good way to avoid this? I have to do this from JScript/Javascript, but could for example do it through a WScript variable (as long as it's called from within JScript). The reason for this is that it's triggered by an event that's evaluated in JScript and not in VBA (VBA is just the wrapper in this case).

I've tried "ThisWorkbook.Application.Quit' (as per https://docs.microsoft.com/en-us/office/vba/api/excel.application.quit ), but that doesn't work either.

Thanks!

1
Could be because you are using a KeyWord. Change ThisWorkBook to any other word MyWorkBook or Something.Mikku
@Mikku - the name of the variable doesn't make a difference (I tried setting it to TWB)JasperD
Okay, one more thing you can do it avoid the Brackets ThisWorkBook.Close FalseMikku

1 Answers

0
votes

This bug only occurs when running the command from the immediate window - if run from within a subroutine it does NOT crash.

I had tested in immediate a bunch of times before realizing this. Non-issue, apologies.