The CreateTOC function regularly crashes InDesign 18.0.
It has worked fine in previous versions of ID (at least up to v17.0).
I've yet to work out the fine details, but some documents I create are fine. Most aren't. Creating the TOC manually seems to work.
The same issue and workaround also occurs with Index.Generate.
The attached file is a .zip file renamed with a .txt extension to try and get it to upload to this supidly restricted site.
Book.indb - this is what the VB script uses to open the other documents
TOC - empty document that the VB script attempt to create the table of contents in
MainDoc - the document containing the entries that will be placed in the table of contents
GenerateIndex.vbs - script that attempts to create the table of contents. Changing showContentsDoc to True allows it to work.
I've managed to narrow down what causes the issue. If the document that is to contain the table of contents is opened with the showingWindow parameter set to False, then the crash can occur. When this is set to True, then creating the TOC appears to work OK.
The attached files illustrate this. The script assumes that the Book.indb file is already open in InDesign. The TOC.indd file should NOT be open.
WARNING: running the script is likely to crash InDesign.
There are other factors that seem to affect whether InDesign will crash or not. The number of TOC entries and the length of the text for the TOC entries influences if CreateTOC will crash or not (e.g. remove the Barrow42 from the end of MainDoc, and CreateTOC seems to work fine - regardless of if the TOC document is hidden or not). Also if the TOC already exists, then CreateTOC does not crash.