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)
mannevenu26Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
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
GrahamSkanRetiredCommented:
You seem to have forgotten to post the document.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

mannevenu26Author Commented:
plz use the test document for above question....US-1---Confi.docx
0
GrahamSkanRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
GrahamSkanRetiredCommented:
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
GrahamSkanRetiredCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.