C gets code sharing for the implementation of all tables in the Word document

  • 2020-10-23 20:16:09
  • OfStack

Today, I generated a data dictionary from the database, but there is no note, so I need the program to read all the tables. Used the following code, pro test available ~~


object oFileName = @"F:\ The database .docx";
object oReadOnly = false ;
object oMissing = System.Reflection.Missing.Value;
 
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
  ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
 
//MessageBox.Show(oDoc.Tables.Count.ToString());
for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
{
  Microsoft.Office.Interop.Word.Table nowTable = oDoc.Tables[tablePos];
  string tableMessage = string.Format(" The first {0}/{1} A table :\n", tablePos, oDoc.Tables.Count);
 
  for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
  {
    for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
    {
      tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
      tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);
      tableMessage += "\t";
    }
 
    tableMessage += "\n";
  }
 
  MessageBox.Show(tableMessage);
}

Related articles: