0
votes

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).

1
I'm afraid I don't understand the last details. I follow that you're extracting cell content, cutting off the end-of-cell characters. But the "how" things aren't working concerning the auto numbering isn't clear. Perhaps if you provide a more exact description of the result you expect vs. the result you're actually getting? FWIW automatic numbering is not returned in Range.Text. You can only access it through the Paragraph and Paragraph.Range objects (the properties that being with "List").Cindy Meister
@CindyMeister I have updated my post by sharing a sample document and the specific issue there. Hope that clarifies it.Akshay Krishnan R
Unable to open the sample document ... The webpage cannot be foundMaarten van Stam
@MaartenvanStam Thanks for pointing out, I have updated the link.Akshay Krishnan R
Blocked by SmartScreen virus protection ... I think (if your file is actually safe) put the document in a zip file for download or us a different file share option (OneDrive?)Maarten van Stam

1 Answers

1
votes

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