/// <summary>
        /// EXTENSION METHOD
        /// Checks - recursively - if the given table is empty
        /// </summary>
        /// <param name="PobjTable"></param>
        /// <returns></returns>
        public static void DeleteEmpty(this Word.Table PobjTable)
        {
            try
            {
                foreach (Word.Table PobjItem in PobjTable.Tables)
                {
                    PobjItem.DeleteEmpty();
                }
                // figure to toal number of characters in this table if it were empty
                // each blank cell has two (2) characters
                int LintTotal = PobjTable.Range.Cells.Count * 2;
                int LintCount = 0;
                foreach (Word.Cell LobjCell in PobjTable.Range.Cells)
                {
                    LintCount += LobjCell.Range.Text.Length;
                }

                // now we see if it is empty and if it is then we delete it
                if (LintTotal >= LintCount)
                {
                    PobjTable.Delete();
                }
                else
                {
                    PobjTable.DeleteRows();
                }
            }
            catch { }
        }
        private void generateIndexPage(byte[] templateLocation, WordInterface wordInterface, Document wordDocument, int projectID)
        {
            lock (this.threadLock)
            {
                try
                {
                    //Open the template from a temporary file.
                    String tempTemplateFile = Path.GetTempFileName();
                    File.WriteAllBytes(tempTemplateFile, templateLocation);
                    Document indexPageTemplate = wordInterface.app.Documents.Open(tempTemplateFile);

                    //Copy template to main document.
                    wordInterface.copyDocumentToOtherDocument(indexPageTemplate, wordDocument, false);
                    wordDocument.Activate();

                    //Find index table template.
                    Microsoft.Office.Interop.Word.Table tableTemplate = wordInterface.findTableWithTitle(wordDocument, "riskAssessmentIndex");
                    if (tableTemplate == null)
                    {
                        throw new Exception("Could not find riskAssessmentIndex table. Check your template.");
                    }

                    //Do not allow the column size to change.
                    tableTemplate.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitFixed);

                    //Get all dagners from the database.
                    DataView dangersView = new DataView(this.tbl_DangerTableAdapter.GetData());

                    //Loop throug the dangers.
                    foreach (DataRowView dangerRow in dangersView)
                    {
                        //Get some dangersource data from the database.
                        int      oldPageCount         = wordDocument.ComputeStatistics(WdStatistic.wdStatisticPages);
                        string   lastDangerSourceName = "";
                        DataView dangerSourceView     = new DataView(this.get_RiskAssessment_Index_DataTableAdapter.GetData(projectID, (Int32)dangerRow["DangerID"]));

                        //Copy table and set the header.
                        Table indexTable = wordInterface.copyTable(wordDocument, tableTemplate.Range);
                        indexTable.Rows[1].Range.Text = dangerRow["DangerGroupName"].ToString();

                        //Add danger sources to the danger group.
                        foreach (DataRowView dangerSourceRow in dangerSourceView)
                        {
                            Row newTableRow;
                            //Create new row when we get a different dangersource then the previous.
                            if (lastDangerSourceName != dangerSourceRow["DangerSourceName"].ToString())
                            {
                                lastDangerSourceName = dangerSourceRow["DangerSourceName"].ToString();
                                newTableRow          = indexTable.Rows.Add(ref missing);
                            }
                            else
                            {
                                newTableRow = indexTable.Rows.Last;
                            }

                            newTableRow.Cells[1].Range.Text = lastDangerSourceName;
                            //Does this dangersource has an risk associated with it?
                            if (dangerSourceRow["RiskID"] != DBNull.Value)
                            {
                                newTableRow.Cells[2].Range.Text = "YES";
                                wordInterface.addTextToTableCell(newTableRow.Cells[3].Range, dangerSourceRow["RiskID"].ToString(), true);
                            }
                            else
                            {
                                newTableRow.Cells[2].Range.Text = "NO";
                            }

                            //Does this dangersource has risks with remaining risk?
                            if (dangerSourceRow["HasRemainingRisk"] != DBNull.Value)
                            {
                                wordInterface.addTextToTableCell(newTableRow.Cells[4].Range, dangerSourceRow["HasRemainingRisk"].ToString(), true);
                            }
                        }
                        //Delete our row template.
                        indexTable.Rows[3].Delete();
                        wordInterface.setAlternatingTableRowStyle(indexTable, ARA_Colors.ARA_Blue1, 4);

                        //Set table on a new pages when it stretches over 2 pages.
                        if (oldPageCount < wordDocument.ComputeStatistics(WdStatistic.wdStatisticPages))
                        {
                            Range tableRange = indexTable.Range;

                            tableRange.SetRange(wordDocument.Content.End - 2, wordDocument.Content.End + 2);
                            //tableRange.SetRange(indexTable.Range.Start - 2, indexTable.Range.Start);
                            wordInterface.insertPageBreakAtRange(indexTable.Range);
                        }
                    }

                    //Delete a trailing enter after our template table.
                    Range rng = tableTemplate.Range;
                    rng.SetRange(rng.End, rng.End + 2);
                    rng.Delete();

                    //Delete our template table and insert a pagebreak.
                    tableTemplate.Delete();
                    wordInterface.insertPageBreakAtRange(wordDocument.Words.Last);

                    // Close and release the Document object.
                    if (indexPageTemplate != null)
                    {
                        ((_Document)indexPageTemplate).Close(ref paramFalse, ref missing,
                                                             ref missing);
                        File.Delete(tempTemplateFile);
                        indexPageTemplate = null;
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ARA_Constants.messageBoxSomethingWrongWhileGenerating + ex.Message, ARA_Constants.messageBoxSomethingWrongWhileGeneratingHeader, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
 /// <summary>
 /// Удаление таблицы
 /// </summary>
 public void DeleteTable()
 {
     _table.Delete();
 }