How to check if word document has tables using C# and VB

← PrevNext →

The Office Interop object in .Net has features that can be used to access Ms-Office objects like Excel, Word etc. In this article I am going to show you how to check if an "Ms-Word" document has a table and if yes, then does the table has columns.

Its common to use a table in Word. Its a convenient way of displaying data (content) in tabular format. We can access Ms-Word contents from our .net application. I'll show you how to check if an Ms-Word document has tables and columns.

First, create a word file and insert a table (or multiple tables) in the document and save the file.

Let's check if word doc has a table.

C# Code

Assuming, the word file is in the "root" directory of your project.

using System;
using Word = Microsoft.Office.Interop.Word;    // For Ms-Word application.

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create Ms-Word objects.
        Word.Application objWord;
        objWord = new Word.Application();
        Word.Document objDoc;

        try
        {
            // Open the Word file in "ReadOnly" mode.
            objDoc = objWord.Documents.Open(Server.MapPath(".") + @"\sample-with-table.docx", ReadOnly: true);

            if (objDoc.Tables.Count != 0)   // Check if Word file has any tables.
            {
                if (objDoc.Tables[1].Columns.Count > 0)     // Check if the table has columns.
                {
                    System.Diagnostics.Debug.WriteLine("columns: " + objDoc.Tables[1].Columns.Count); // Get total columns in table.
                    System.Diagnostics.Debug.WriteLine("rows: " + objDoc.Tables[1].Rows.Count);  // Get total rows.
                }
            }
        }
        catch (Exception ex)
        {   
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
        finally
        {
            // Clean up.
            objWord.Quit(); objWord = null;
            objDoc = null;
        }
    }
}

Run the application. Make sure the word file is in the root directory for the project.

Note: You can see the result in the "Output" window of the IDE.
To open the Output window, select Debug -> Windows (from top menu) and click the "Output" option.

open output window in visual studio ide

Now, lets see How to import table data in Ms-Word to a GridView control.

Vb Code
Option Explicit On
Imports Word = Microsoft.Office.Interop.Word    ' For Ms-Word application.

Partial Class Site
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' Create Ms-Word objects.
        Dim objWord As Word.Application
        objWord = New Word.Application
        Dim objDoc As Word.Document

        Try
            ' Open word file in "ReadOnly" mode.
            objDoc = objWord.Documents.Open(Server.MapPath(".") & "\sample-with-table.docx", [ReadOnly]:=True)

            If objDoc.Tables.Count <> 0 Then    ' Check if doc has tables.
                If objDoc.Tables(1).Columns.Count > 0 Then  ' Now, check if the table has columns.
                    System.Diagnostics.Debug.WriteLine("columns: " & objDoc.Tables(1).Columns.Count) ' Get total columns in table.
                    System.Diagnostics.Debug.WriteLine("rows: " & objDoc.Tables(1).Rows.Count)  ' Get total rows.
                End If
            End If
        Catch ex As Exception
            System.Diagnostics.Debug.WriteLine(ex.Message)
        Finally
            ' Clean up.
            objWord.Quit() : objWord = Nothing
            objDoc = Nothing
        End Try
    End Sub
End Class

← PreviousNext →