Solved

read tables in document with specific section

Posted on 2014-10-16
13
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

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!

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

751 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