Search code examples
c#ms-wordcell

C# MS Word: How to get page number where a table cell starts and where it ends?


I need to find out whether or not a table cell is spread across more than one pages. My idea was simply to get the page number where the row starts and where it ends. For gettings the start and end page I wrote following two methods:

 public static long GetCellStartPageNo(Word.Cell cell)
    {
        long result = -1;
        cell.Select();
        cell.Application.Selection.Start = 0;
        cell.Application.Selection.End = 1;
        result = cell.Application.Selection.Information[Word.WdInformation.wdActiveEndPageNumber];
        return result;
    }



public static long GetCellEndPageNo(Word.Cell cell)
    {
        long result = -1;
        cell.Select();
        int len = cell.Application.Selection.Text.Length;
        cell.Application.Selection.Start = 0;
        cell.Application.Selection.End = len - 1;
        result = cell.Application.Selection.Information[Word.WdInformation.wdActiveEndPageNumber];
        return result;
    }

The idea behind the two methods is simple: select the first character in the cell and get the selection range's page number (=> start page), then select the last character in the cell and get the selection range's page number again (=> end page). Unfortunately, both methods always return 1 (=page 1) as result. What am I doing wrong?

Thanks in advance, Michael


Solution

  • I think the problem is this part

    cell.Application.Selection.Start = 0;
    cell.Application.Selection.End = 1;
    

    Selection.start 0 is the begin of the document, not of the table.

    You need something like

    cell.select();
    cell.Application.Selection.End=cell.Application.Selection.Start+1;
    

    cell.Application.Selection.Information[Word.WdInformation.wdActiveEndPageNumber]; should now return the page where the table starts.