Add Row to Existing Table using C#

D

DavidSp8

I'd like to automate a word process that inserts a row into an existing table
in a word document. The table is always preceded by some text that I can
find in the document. I have already got some things working using
automation and c#, but i cannot figure out how to find this table and add a
row to it using automation?

Does anybody know how to accomplish this?

Thanks
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RGF2aWRTcDg=?=,
I'd like to automate a word process that inserts a row into an existing table
in a word document. The table is always preceded by some text that I can
find in the document. I have already got some things working using
automation and c#, but i cannot figure out how to find this table and add a
row to it using automation?
Assuming you have only the one table in the document, "grabbing it" would be
something like:

word.table tbl=doc.Tables(1);
//add a row at the end
tbl.Rows.Add();

Note that C# might force you to include the optional BeforeRow parameter. If thi
is the case, you need to declare an object variable:
object missingValue = Type.Missing;
then
tbl.Rows.Add(ref missingValue);

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
D

DavidSp8

I don't have just one table. I have many tables. The table that I need to
add a row to consistently has specific text prior to the table, so in word I
can do a find to locate the table.

How do I get a reference to the Table, i've tried using a find, but I can't
seem to get a reference to the table.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RGF2aWRTcDg=?=,
I don't have just one table. I have many tables. The table that I need to
add a row to consistently has specific text prior to the table, so in word I
can do a find to locate the table.

How do I get a reference to the Table, i've tried using a find, but I can't
seem to get a reference to the table.
OK, you do a RANGE.FIND, I take it? And how close is the table to the end of
the "found" range? (In the document, how many times do you have to press
right-arrow?) Or would it be directly under the paragraph where the "found"
range is?

When you use Range.Find, and Find.Execute is successful, Range contains exactly
the Find.Text range (in other words, the location of Range changes). So, if the
table were directly after the paragraph where the found range is, I'd do:

Range = Range.Paragraphs(1).Range;
Range.collapse(0); /wdCollapseEnd
//range should now be inside the table
Word.Table tbl = Range.Tables(1);

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
D

DavidSp8

I was not using the Range, I was doing a doc.Content.Find which was not
working. The Range works great for me I was able to get it done using your
example!

THANKS
 

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