Ok thanks,
I added the following code
$docx->addText('Contents',array('pStyle' => 'Heading1'));
$docx->addText(' ',array('pStyle' => 'Heading1Line'));
$legend = array(
'text' => 'Click here to update the TOC',
'color' => 'B70000',
'bold' => true,
'fontSize' => 12,
);
$docx->addTableContents(array('autoUpdate' => true,'displayLevels' => '2-4'),$legend);
$docx->addBreak(array('type' => 'page'));
and the TOC is generated following the other contents. But when I open the generated file and try to update the TOC, I get the following error 'Creating a table of contents? Start by applying a heading style from the Styles gallery to the selected text. Any idea guys what this could be ?