title: C# How to write each table in a Word file to its own Excel file
body: <p>I'm trying to write code in C# WinForms that allows a user to select a directory tree, and extract all of the table data from a word document into an excel file. Presently, the code compiles and you can select your directories, etc, but once it begins to iterate through the loop for each table it crashes.</p>
  
  <p>The program successfully opens the first word file and writes the first excel file (table_1_whatever.xlsx) and saves it in the destination folder. However, on the second table in the same file I get this error on this line of code:</p>
  
  <pre><code>    worksheet.Cells[row, col] = objExcelApp.WorksheetFunction.Clean(table.Cell(row, col).Range.Text);
  </code></pre>
  
  <p>System.Runtime.InteropServices.COMException: 'The requested member of the collection does not exist.' </p>
  
  <p>I can't seem to figure out why it doesn't exist. Each time it goes through the foreach loop it should be creating a new worksheet, but it doesn't appear to be working. Any insight, examples, or suggestions are welcome!</p>
  
  <p>Code:</p>
  
  <pre><code>    private void WordRunButton_Click(object sender, EventArgs e)
      {
  
          var excelApp = new excel.Application();
          excel.Workbooks workbooks = excelApp.Workbooks;
          var wordApp = new word.Application();
          word.Documents documents = wordApp.Documents;
          wordApp.Visible = false;
          excelApp.Visible = false;
  
          string[] fileDirectories = Directory.GetFiles(WordSourceBox.Text, "*.doc*",
                 SearchOption.AllDirectories);
  
          foreach (var item in fileDirectories)
          {
              word._Document document = documents.Open(item);
              int tableCount = 1;
  
              foreach (word.Table table in document.Tables)
              {
                  if (table.Cell(1, 1).ToString() != "Doc Level")
                  {
                      string wordFile = item;
                      appendName = Path.GetFileNameWithoutExtension(wordFile) + "_Table_" + tableCount + ".xlsx";
  
                      var workbook = excelApp.Workbooks.Add(1);
                      excel._Worksheet worksheet = (excel.Worksheet)workbook.Sheets[1];
  
                      for (int row = 1; row &lt;= table.Rows.Count; row++)
                      {
                          for (int col = 1; col &lt;= table.Columns.Count; col++)
                          {
                              var cell = table.Cell(row, col);
                              var range = cell.Range;
                              var text = range.Text;
  
                              var cleaned = excelApp.WorksheetFunction.Clean(text);
  
                              worksheet.Cells[row, col] = cleaned;
                          }
                      }
                      workbook.SaveAs(Path.Combine(WordOutputBox.Text, Path.GetFileName(appendName)), excel.XlFileFormat.xlWorkbookDefault);
  
                      workbook.Close();
                      Marshal.ReleaseComObject(workbook);
                  }
                  else
                  {
                      WordOutputStreamBox.AppendText(String.Format("Table {0} ignored\n", tableCount));
                  }
  
                  WordOutputStreamBox.AppendText(appendName + "\n");
                  tableCount++;
              }
  
              document.Close();
              Marshal.ReleaseComObject(document);
              WordOutputStreamBox.AppendText(item + "\n");
          }
  
  
          WordOutputStreamBox.AppendText("\nAll files parsed");
  
          excelApp.Application.Quit();
          workbooks.Close();
          excelApp.Quit();
  
          WordOutputStreamBox.AppendText("\nExcel files closed");
  
          Marshal.ReleaseComObject(workbooks);
          Marshal.ReleaseComObject(excelApp);
          WordOutputStreamBox.AppendText("\nExcel files released");
  
          wordApp.Application.Quit();
          wordApp.Quit();
          WordOutputStreamBox.AppendText("\nWord files have been quit");
  
          Marshal.ReleaseComObject(documents);
          Marshal.ReleaseComObject(wordApp);
          WordOutputStreamBox.AppendText("\nWord files have been released\n");
      }
  </code></pre>
  
  <p>Edit 1:(Sorry for posting in the wrong place the first time!)</p>
  
  <p>Ok, so the problem has been isolated...</p>
  
  <p>The code logic of the code was fine, and the table was in fact there. The issue is that the second table of these files has a set of split cells in it, so, when it reaches the cell that contains it, the program crashes.</p>
  
  <p>As a temp fix, I have just set it to ignore the table if the header == whatever. Does anyone know of a solution that actually allows to extract this data though?</p>