Solved

read tables in document with specific section

Posted on 2014-10-16
13
186 Views
Last Modified: 2014-11-18
Hi All,


How to fetch table rows and columns data from ms word document in particular section ?(not all document tables)
0
Comment
Question by:mannevenu26
  • 7
  • 5
13 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40403961
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
 

Author Comment

by:mannevenu26
ID: 40405744
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40406031
You seem to have forgotten to post the document.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:mannevenu26
ID: 40406075
plz use the test document for above question....US-1---Confi.docx
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40406109
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
 

Author Comment

by:mannevenu26
ID: 40406161
Hi GrahamSkan,

please go through the document..mycode.docx

I have written my .net code

thanks
0
 

Author Comment

by:mannevenu26
ID: 40408051
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40408132
What is the error, please?
0
 

Author Comment

by:mannevenu26
ID: 40408163
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40408212
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
 

Author Comment

by:mannevenu26
ID: 40408309
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
 

Author Comment

by:mannevenu26
ID: 40413219
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question