3

When I use the below line It reads all tables of that particular document:

  foreach (Microsoft.Office.Interop.Word.Table tableContent in document.Tables)

But I want to read tables of a particular content for example from one identifier to another identifier.

Identifier can be in the form of [SRS oraganisation_123] to another identifier [SRS Oraganisation_456]

I want to read the tables only in between the above mentioned identifiers.

Suppose 34th page contains my identifier so I want read all tables from that point to until I come across my second identifier. I don't want to read remaining tables.

Please ask me for any clarification in the question.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Charan Gourishetty
  • 109
  • 1
  • 2
  • 10

3 Answers3

0

Go through following code, if it helps you.

System.Data.DataTable dt = new System.Data.DataTable();
           foreach (Microsoft.Office.Interop.Word.Cell c in r.Cells)
            {
                if(c.Range.Text=="Content you want to compare")
                  dt.Columns.Add(c.Range.Text);
            }
            foreach (Microsoft.Office.Interop.Word.Row row in newTable.Rows)
            {
                System.Data.DataRow dr = dt.NewRow();
                int i = 0;
                foreach (Cell cell in row.Cells)
                {
                    if (!string.IsNullOrEmpty(cell.Range.Text)&&(cell.Range.Text=="Text you want to compare with"))
                    {
                        dr[i] = cell.Range.Text; 
                    }
                }
                dt.Rows.Add(dr);
                i++;
            }

Go through following linked 3rd number answer.

Replace bookmark text in Word file using Open XML SDK

Community
  • 1
  • 1
Freelancer
  • 9,008
  • 7
  • 42
  • 81
0

Not sure how your program is structured... but if you can access the identifier in tableContent then you should be able to write a LINQ query.

var identifiers = new List<string>();
identifiers.Add("myIdentifier");

var tablesWithOnlyTheIdentifiersIWant = document.Tables.Select(tableContent => identifiers.Contains(tableContent.Identifier)

foreach(var tableContent in tablesWithOnlyTheIdentifiersIWant)
{
     //Do something
}
Chutes
  • 123
  • 1
  • 8
0

Say start and end Identifiers are stored in variables called myStartIdentifier and myEndIdentifier -

    Range myRange = doc.Range();
    int iTagStartIdx = 0;
    int iTagEndIdx = 0;

    if (myRange.Find.Execute(myStartIdentifier))
        iTagStartIdx = myRange.Start;

    myRange = doc.Range();    
    if (myRange.Find.Execute(myEndIdentifier))
        iTagEndIdx = myRange.Start;

    foreach (Table tbl in doc.Range(iTagStartIdx,iTagEndIdx).Tables)
    {
       // Your code goes here
    }
Arun
  • 969
  • 7
  • 17