Search code examples
.netms-wordoffice-interop

Determining Distance Between Two Tables Word Interop


I have Word documents that data needs to be extracted from, full of tables. However in many cases the one table, that continues for many pages, has actually been created as multiple tables. I am using Office 2016.

So:

document.Tables[1] ; Table #1 - Page 1
document.Tables[2] ; Table #1 - Page 2
document.Tables[3] ; Table #2 - Page 2

Are there some properties I can use on these table objects to determine any of the following:

  • Page number of first / last row in the table
  • What is the text between table #1 and table #2 (i.e. so i can look for newlines, etc)

I can retrieve width/height of table, but checking the available properties at MSDN here there doesn't seem any obvious way to reference more specific position.


Solution

    1. The Range object has an Information property that you give a parameter to return the page number. If this is C#, since that language doesn't "like" using parameters with properties, it's the get_Information method.

    So something along the lines of

    Word.Range rngRow1 = Table1.Rows[1].Range;
    int pgNr = rngRow1.get_Information(Word.WdInformation.wdActiveEndAdjustedPageNumber);
    
    1. You can collapse a Range in order to put the "focus" just after that Range. In the case of a table, that puts the focus immediately after the table, in the following Paragraph. Note that, in most recent versions of Word, this Paragraph is required and carries some of the table formatting properties. Of course, there can be more than one paragraph, with many lines...

    One possibility to pick up the Range between two tables would look something like the following. Once you have the Range you can analyze what it contains (or just delete it to merge the two tables).

    Word.Range rngBetweenTables = Table1.Range;
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rngBetweenTables.Collapse(ref oCollapseEnd);
    Word.Range rngStartOfNextTable = Table2.Range;
    object oCollapseStart = Word.WdCollapseDirection.wdCollapseStart;
    rngStartOfNextTable.Collapse(ref oCollapseStart);
    //Extends the Range to the specified position
    //Think of it like holding the Shift key, then clicking to extend a selection
    rngBetweenTables.End = rngStartOfNextTable.Start - 1;