Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

read tables in document with specific section

Hi All,


How to fetch table rows and columns data from ms word document in particular section ?(not all document tables)
0
mannevenu26
Asked:
mannevenu26
  • 7
  • 5
1 Solution
 
GrahamSkanCommented:
Word document sections can be accessed via their index. You can also use the table index to access a particular table in the section. Here is some illustrative code.
                objWord = new Word.Application();
                objWord.Visible = true;
                // Open the Document               
                objDoc = objWord.Documents.Open("C:\\Users\\UserName\\Documents\\Doc1.docx",ref missing, ref missing, ref missing, ref missing);
    
                objSecs = objDoc.Sections;
                objSec = objSecs[2];
                objRange = objSec.Range;
                objTable = objRange.Tables[1];

                oCellData = objTable.Cell(1, 2).Range.Text;
                Console.WriteLine("Text in Row1, Column2 = " + oCellData);
                System.Threading.Thread.Sleep(5000);

Open in new window

0
 
mannevenu26Author Commented:
Thanks GrahamSkan, but I am not finding the proper solution...not getting the Glossary section ...

I have attached a word document,
In this document a Glossary section is there and inside the glossary section a table is there, so I want to get the particular table data.


Thanks
0
 
GrahamSkanCommented:
You seem to have forgotten to post the document.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mannevenu26Author Commented:
plz use the test document for above question....US-1---Confi.docx
0
 
GrahamSkanCommented:
Your document has four Sections. The last one starts after the 'Table of Contents'. In it there is a paragraph with the style 'Heading 1' and with text "Glossary". The table that follows it is the seventh table in the section, so I suppose that that is the table that you want to read.

If so you will need those numbers (4 and 7) to be in your code:
                objWord = new Word.Application();
                objWord.Visible = true;
                // Open the Document               
                objDoc = objWord.Documents.Open("C:\\Users\\UserName\\Documents\\Doc1.docx",ref missing, ref missing, ref missing, ref missing);
    
                objSecs = objDoc.Sections;
                objSec = objSecs[4];
                objRange = objSec.Range;
                objTable = objRange.Tables[7];

                oCellData = objTable.Cell(1, 2).Range.Text;
                Console.WriteLine("Text in Row1, Column2 = " + oCellData);
                System.Threading.Thread.Sleep(5000);

Open in new window

0
 
mannevenu26Author Commented:
Hi GrahamSkan,

please go through the document..mycode.docx

I have written my .net code

thanks
0
 
mannevenu26Author Commented:
Hi GrahamSkan,

I am getting error here, can u plz help me ?

 Sections objSecs = doc.Sections;
                Section objSec = objSecs[4];
                Range objRange = objSec.Range;
                Table objTable = objRange.Tables[7]; //error
                Cell oCellData = objTable.Cell(1, 2).Range.Text;
                Console.WriteLine("Text in Row1, Column2 = " + oCellData);
                System.Threading.Thread.Sleep(5000);


thanks
0
 
GrahamSkanCommented:
What is the error, please?
0
 
mannevenu26Author Commented:
Hi GrahamSkan,

This is my code. I am not getting any error but not getting proper output.
I want to filter specific columns data for particular section .......


 string fileName = Path.GetFullPath(FileUpload1.PostedFile.FileName);
            Microsoft.Office.Interop.Word.Application msWordApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = msWordApp.Documents.Open(fileName, null, true);
                        bool glossaryStatus = false;bool headStatus = false;
            List<string> glossaryText = new List<string>();
            List<string> glossaryHeadText = new List<string>();
            List<string> glossaryTableText = new List<string>();

            DataTable dt=new DataTable();
            DataColumn dc = new DataColumn();
            DataRow dr;

                       
            foreach (Paragraph paragraph in doc.Content.Paragraphs)
            {
                Microsoft.Office.Interop.Word.Style style = paragraph.get_Style() as Microsoft.Office.Interop.Word.Style;
                string styleName = style.NameLocal;
                string textTemp = paragraph.Range.Text;
                string text = textTemp.Replace("\r", "").Replace("\a", "").Replace("\t", "").Replace("\f", "").Replace("/", "");
                if ((styleName == "Heading 1" || styleName == "Heading 2" || styleName == "Heading 3") && text == "Glossary")
                {

                    glossaryStatus = true;

                }
                else if (styleName == "Heading 1" || styleName == "Heading 2" || styleName == "Heading 3")
                {
                    glossaryStatus = false;
                }

                if (((styleName == "Table_Text") || (styleName == "Table_Head") || (styleName == "Paragraph_Text") || (styleName == "Normal")) && glossaryStatus)
                {
                    Sections objSecs = doc.Sections;
                    Section objSec = objSecs[4];
                    Range objRange = objSec.Range;
                    Microsoft.Office.Interop.Word.Table objTable = objRange.Tables[7];

                    //Rows rows=objTable.Rows
                    string oCellData = objTable.Cell(1, 2).Range.Text;


                    totalTableText = totalTableText + 1;
                    glossaryTableText.Add(text);
                    if (styleName == "Table_Head" && !string.IsNullOrWhiteSpace(text))
                    {
                           
                            glossaryHeadText.Add(text);

                    }      

                }
               
                //Console.WriteLine("Text in Row1, Column2 = " + oCellData);
                //System.Threading.Thread.Sleep(5000);
            }
           
           







Thanks
0
 
GrahamSkanCommented:
I'm not sure that I understand. I have shown you how to read the text from a cell in a table. If you want to read all of the cells in a column then you will have to create a loop that increment the row index in the Table.Cell() method. For all the data in a row you would need similar loop that increments the column index.

Please note that I am more of a Word expert than a C# expert, and normally code in VBA.
0
 
mannevenu26Author Commented:
Hi GrahamSkan,

US-1---Confi.docx
In my document, these are all sections(headings1,2,3)

1      USER STORY FOR AUTOMATION FRAMEWORK CONFIGURATION
1.1      Story Card
1.2      Story Card
1.3      Acceptance Criteria      
1.4      Algorithm Configuration Status
1.4.1      Business Rules/Processing Rules/Validations
1.5      Default Framework Configuration
1.5.1      Business Rules/Processing Rules/Validations      
1.6      Default Framework Configuration      
1.6.1      Business Rules/Processing Rules/Validations      
2      Glossary      


I want check all table data present inside the each headings.
But in your code all are coming under section 4,so that I am not getting the Glossary section.



one more thing......I not getting total number of rows & columns using Count method
0
 
mannevenu26Author Commented:
Hi GrahamSkan,


I am not able to set particular glossary section range ....

every it checks all tables in section4..... plz help me ..


 for (int s = 1; s <= objSecs.Count; s++)
               {
                   Section objSec = objSecs[s];
                   Range objRange = objSec.Range;
for (int t = 1; t <= objRange.Tables.Count; t++)
                   {
                       Microsoft.Office.Interop.Word.Table objTable = objRange.Tables[t];
                       string col1 = ""; string col2 = ""; int c1 = 0; int c2 = 0;
                       
                       for (int i = 1; i <= objTable.Columns.Count; i++)
                       {
                           string oCellData = objTable.Cell(1, i).Range.Text.Replace("\r", "").Replace("\a", "").Replace("\t", "").Replace("\f", "").Replace("/", "");
                           if (TextBox1.Text == oCellData)
                               col1 = oCellData;
                           if (TextBox2.Text == oCellData)
                               col2 = oCellData;
                       }
                       if (!string.IsNullOrWhiteSpace(col1) && !string.IsNullOrWhiteSpace(col2))
                       {
                           for (int i = 0; i < glossaryHeadText.Count; i++)
                           {
                                                                 c2 = i + 1;
                           }
                           if (c1 != 0 && c2 != 0)
                           {
                               for (int i = 2; i <= objTable.Rows.Count; i++)
                               {
                                   string xxx = objTable.Cell(i, c1).Range.Text.Replace("\r", "").Replace("\a", "").Replace("\t", "").Replace("\f", "").Replace("/", "");
                                   string yyy = objTable.Cell(i, c2).Range.Text.Replace("\r", "").Replace("\a", "").Replace("\t", "").Replace("\f", "").Replace("/", "");
                                   if (!string.IsNullOrWhiteSpace(xxx) && !string.IsNullOrWhiteSpace(yyy))
                                       nonEmptyRows = nonEmptyRows + 1;
                                   totalRows = totalRows + 1;
                               }
                           }
                       }
                       
                   }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now