none
Get all tables values which exist in word document RRS feed

  • Question

  • Hi,

    We need to extract all tables (from word document) and insert into a SQL server table. Can anyone help to get rows/columns from word document for each table?

    See below what i'm trying...

    using (WordprocessingDocument doc = WordprocessingDocument.Open(@"D:\DocFiles\LMA3.docx", true))
                {
                    IEnumerable<Paragraph> paragraphElement = doc.MainDocumentPart.Document.Descendants<Paragraph>();
                    foreach (OpenXmlElement section in doc.MainDocumentPart.Document.Body.Elements<OpenXmlElement>())
                    {                    
                        if (section.GetType().Name == "Paragraph")
                        {
                            Paragraph par = (Paragraph)section;
                                                }
                        else if (section.GetType().Name == "Table")
                        {
                            Table tab = (Table)section;
                        }
                    }

    Friday, August 31, 2018 1:29 PM

Answers

  • Hi Lokesh,

    There is an error when I run your code in my envirment.

    I’ve rewirte code as blew, please try.

    object oFileName = @"C:\Users\XXXX\Desktop\A.docs";
    
    object oReadOnly = true;
    
    object oMissing = System.Reflection.Missing.Value;
    
    Microsoft.Office.Interop.Word._Application oWord;
    
    Microsoft.Office.Interop.Word._Document oDoc;
    
    oWord = new Microsoft.Office.Interop.Word.Application();
    
    oWord.Visible = true;//for inspection
    
    oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
    
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
    foreach (Microsoft.Office.Interop.Word.Table table in oDoc.Tables)
    
    {
    
        for (int row = 1; row <= table.Rows.Count; row++)
    
        {
    
            for (int col = 1; col <= table.Columns.Count; col++)
    
            {
    
                // This is the data in the rows of the table, and this is what you need to do
    
                string data = table.Cell(row, col).Range.Text;
    
            }
    
        }
    
    }

    Best Regards,

    Bruce



    • Proposed as answer by Simon Wu-MSFT Tuesday, September 4, 2018 8:55 AM
    • Marked as answer by Lokesh Sharma1 Tuesday, September 4, 2018 9:03 AM
    Monday, September 3, 2018 8:46 AM
    Moderator

All replies

  • Hi Lokesh,

    There is an error when I run your code in my envirment.

    I’ve rewirte code as blew, please try.

    object oFileName = @"C:\Users\XXXX\Desktop\A.docs";
    
    object oReadOnly = true;
    
    object oMissing = System.Reflection.Missing.Value;
    
    Microsoft.Office.Interop.Word._Application oWord;
    
    Microsoft.Office.Interop.Word._Document oDoc;
    
    oWord = new Microsoft.Office.Interop.Word.Application();
    
    oWord.Visible = true;//for inspection
    
    oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
    
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
    foreach (Microsoft.Office.Interop.Word.Table table in oDoc.Tables)
    
    {
    
        for (int row = 1; row <= table.Rows.Count; row++)
    
        {
    
            for (int col = 1; col <= table.Columns.Count; col++)
    
            {
    
                // This is the data in the rows of the table, and this is what you need to do
    
                string data = table.Cell(row, col).Range.Text;
    
            }
    
        }
    
    }

    Best Regards,

    Bruce



    • Proposed as answer by Simon Wu-MSFT Tuesday, September 4, 2018 8:55 AM
    • Marked as answer by Lokesh Sharma1 Tuesday, September 4, 2018 9:03 AM
    Monday, September 3, 2018 8:46 AM
    Moderator
  • But Interop.Word doesn't work with DOCX files.

    Thats why we took approach of OPENXML...

    Tuesday, September 4, 2018 9:05 AM
  • Getting this error, if i run it

    System.Runtime.InteropServices.COMException: The requested member of the collection does not exist.

    string data = table.Cell(row, col).Range.Text;

    Tuesday, September 4, 2018 9:22 AM
  • Hi Lokesh,

    What type of project were you trying to create?

    I created a console application and added dependencies

    I was able to get data after I run application.

    Added dependencies as follows: 

    The complete code is below, please try.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                object oFileName = @"C:\Users\xxx\Desktop\1.docx";
                object oReadOnly = true;
                object oMissing = System.Reflection.Missing.Value;
    
                Microsoft.Office.Interop.Word._Application oWord;
                Microsoft.Office.Interop.Word._Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = true;
                oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
               
                foreach (Microsoft.Office.Interop.Word.Table table in oDoc.Tables)
                {
                    for (int row = 1; row <= table.Rows.Count; row++)
                    {
                        for (int col = 1; col <= table.Columns.Count; col++)
                        {
                            // This is the data in the rows of the table, and this is what you need to do
                            string data = table.Cell(row, col).Range.Text;
                            Console.WriteLine(data);
    
                        }
                    }
                }
                Console.ReadKey();
            }
        }
    }

    Best Regards,

    Bruce


    Wednesday, September 5, 2018 3:17 AM
    Moderator