Akshay Krishnan R
Akshay Krishnan R

Reputation: 443

Extracting cell values from columns of word document tables with automatic numbering

I'm developing a word automation based application using Microsoft.Office.Interop.Word
I'm reading data from cells using this approach:

string strValue = tbMytable.Rows[iRowIndex].Cells[1].Range.Text;
if (strValue.Contains("\x0d\x07"))
{
    // remove cell marker
    strValue = strValue.Remove(strValue.IndexOf("\x0d\x07"), 2);
}

This worked for most of the tables but once I got an empty string returned by tbMytable.Rows[iRowIndex].Cells[1].Range.Text

When I checked manually by opening the document in Microsoft Office Word 2013, I could see values for the first column cells as step 1, step 2 etc.

Upon some debugging, I figured out that it's because that table used automatic numbering for the first column (the step number string was 1 range wide). I opened the document in Word 2013 again and confirmed this.

Now how do I read the value from such a cell using Word Interop ?
And is there any better approach for extracting vales from cells without the cell marker other than trimming \x0d\x07 or doing a clmyCell.Range.MoveEnd(WdUnits.wdCharacter, -1) ?


EDIT:
I have added a sample document [1] for clarification.

(password for zip is password , without any whitespace).

I am able to read data from all cells except the cells in the first column.
How do I read data from cells in first column (eg. the "Step 2" string) using word interop ?

[1]     http://www.fileconvoy.com/dfl.php?id=g25ad0529c423888e999811789c1cece853a016e55



(password for zip is password , without any whitespace).

Upvotes: 0

Views: 2526

Answers (1)

Maarten van Stam
Maarten van Stam

Reputation: 1899

I tested it on your document and here is a small example to show you how it is done in VBA

Sub ShowTableCellValue()

    If ActiveDocument.Tables(1).Cell(1, 1).Range.ListParagraphs.Count > 0 Then
        MsgBox ActiveDocument.Tables(1).Cell(1, 1).Range.ListFormat.ListString
    End If

End Sub

I guess you can translate it yourself to C#, if not shout out and I'll help you to do that as well

Upvotes: 1

Related Questions