# What can I do when GenerateDocument fails to work from within in a complex interface?

I have found that when GenerateDocument is called from a button within a complex interface that the function does not work, i.e. nothing happens, and the front end "stalls".

GenerateDocument["ExampleData/BasicTemplate.nb", <|"author" -> "Henry Jones"|>


I use the term "stall"s because the symptoms are different from a front end freeze where you typically, on OS X, see the spinning rainbow wheel. The front end is inactive for a period of time but no rainbow wheel.

GenerateDocument typically wants to create a temporary cell with a progress bar, so if that is not possible within the interface could this be the cause of the problem? If so how do I fix it?

• It involves FE to finish so my guess is Method -> "Queued" should help. And it makes a difference on Windows. Does it work for you? – Kuba Oct 23 '18 at 6:40
• @Kuba It makes no difference. No "obvious" things worked until I spelunked the function. – Mike Honeychurch Oct 23 '18 at 8:40
• I see, anyway for completeness, the example from the question in Windows gives 'packet error' for regular button but works well with Queued Method. – Kuba Oct 23 '18 at 8:44
• The entire button operation in this interface, of which GenerateDocument is just a part would never work without Method->"Queued" due to the length of time taken. Additionally the answer described here does not take any longer than the docs example without these options. The only difference is bypassing UsingFrontEnd – Mike Honeychurch Oct 23 '18 at 10:57

The internal functions being used by GenerateDocument is GenerateDocumentDriver, which has 4 options, "PostProcess" -> True,"HeadlessMode" -> False,"ProgressIndicator" -> True,"RetainAsTemplate" -> False.

"HeadlessMode" is used by another internal function postProcessReplacedNotebook. The "HeadlessMode" option controls whether UsingFrontEnd is being used. It appears that when this function is used it can "inhibit" other front end behaviour at times.

This code allows everything to work but doesn't quite get us the desired result:

GenerateDocument["ExampleData/BasicTemplate.nb", <|"author" -> "Henry Jones"|>,

Wrapping with CreateDocument fixes everything and produces the desired output without any front end issues:
CreateDocument@GenerateDocument["ExampleData/BasicTemplate.nb", <|"author" -> "Henry Jones"|>,