How to get the value of a cell from a table?

V

Vinix

There are 3 table in the doc file. They are just pasted from another file.
Now I want to change the text in the first cell of each row, but when I want
to get the text, I got COMExceptrion from "CurrRow.Cells[0]" which says
member needed in the collection is not existed (something like that, I
translated the message from Chinese).

Below is my code snippet in C# that cause the exception:

using Word = Microsoft.Office.Interop.Word;

...(Code omitted)...

foreach (Word.Table CurrTable in docNewFile.Tables)
{
foreach (Word.Row CurrRow in CurrTable.Rows)
{
string OriginalText = CurrRow.Cells[0].Range.Text;
CurrRow.Cells[0].Range.Text = "HeaderString" + OriginalText;
}
}

Is there anything wrong? I'm using VS2005 C#.
 
D

Doug Robbins - Word MVP

I don't know anything about C#, but in vba.

You would use

Dim i as Long, j as Long
With ActiveDocument
For i = 1 to .Tables.Count
For j = 1 to .Tables(i).Rows.Count
.Tables(i).Cell(j, 1).Range.Text = "The New Text"
Next j
Next i
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Russ

Vinix,
Arrays in Word may start with 0, but collections, like Cells(), start with
an index of 1.
There are 3 table in the doc file. They are just pasted from another file.
Now I want to change the text in the first cell of each row, but when I want
to get the text, I got COMExceptrion from "CurrRow.Cells[0]" which says
member needed in the collection is not existed (something like that, I
translated the message from Chinese).

Below is my code snippet in C# that cause the exception:

using Word = Microsoft.Office.Interop.Word;

...(Code omitted)...

foreach (Word.Table CurrTable in docNewFile.Tables)
{
foreach (Word.Row CurrRow in CurrTable.Rows)
{
string OriginalText = CurrRow.Cells[0].Range.Text;
CurrRow.Cells[0].Range.Text = "HeaderString" + OriginalText;
}
}

Is there anything wrong? I'm using VS2005 C#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top