Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading Tables in C#

164 views
Skip to first unread message

randy1200

unread,
Aug 11, 2006, 11:11:34 AM8/11/06
to
The method below works great for reading Word documents into a C# program.
After reading in, I'm able to make substituions to the text in the
paragraphs, and output for a new document, which is very useful.

The obivous problem with this is that it ignores tables. Can anyone
suggestion how to loop though a document to read paragraphs and tables? Or
perhaps point me toward an example on the web?

I need to be able to read in paragraphs and tables in sequence, so I can
output those paragraphs and tables in the same sequence.

Thanks,
Randy


private void GetExistingParagraphsPost(Word._Document oDoc)
{
ExistingParagraphs.Clear();
Console.WriteLine(oDoc.Paragraphs.Count);
Console.WriteLine(oDoc.Tables.Count);
foreach (Word.Paragraph p in oDoc.Paragraphs)
{
Word.Range rng = p.Range;
Word.Style styl = rng.get_Style() as Word.Style;
Console.WriteLine(styl.NameLocal);
ExistingParagraphs.Add(styl, p.Range.Text.ToString());
}
}


Cindy M.

unread,
Aug 11, 2006, 12:50:40 PM8/11/06
to
Hi Randy,

> The method below works great for reading Word documents into a C# program.
> After reading in, I'm able to make substituions to the text in the
> paragraphs, and output for a new document, which is very useful.
>
> The obivous problem with this is that it ignores tables. Can anyone
> suggestion how to loop though a document to read paragraphs and tables? Or
> perhaps point me toward an example on the web?
>
> I need to be able to read in paragraphs and tables in sequence, so I can
> output those paragraphs and tables in the same sequence.
>

Test whether the range is in a table (range.information(wdwithintable) - but
put it into c# speak :-)). If it is, set up an inner loop that goes through
all the cells in the table, then you can get all the paras in each cell.

>
> Private void GetExistingParagraphsPost(Word._Document oDoc)


> {
> ExistingParagraphs.Clear();
> Console.WriteLine(oDoc.Paragraphs.Count);
> Console.WriteLine(oDoc.Tables.Count);
> foreach (Word.Paragraph p in oDoc.Paragraphs)
> {
> Word.Range rng = p.Range;
> Word.Style styl = rng.get_Style() as Word.Style;
> Console.WriteLine(styl.NameLocal);
> ExistingParagraphs.Add(styl, p.Range.Text.ToString());
> }
> }
>

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :-)

Peter Huang [MSFT]

unread,
Aug 16, 2006, 2:11:55 AM8/16/06
to
Hi Randy,

I agree with Cindy's suggestion, for your reference, here is some c# code
snippet.
Application wdApp = new Application();
wdApp.Visible = true;
object o = System.Reflection.Missing.Value;
object path = @"c:\test\test.doc";
Document oDoc = wdApp.Documents.Open(ref path, ref o, ref o,
ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref
o, ref o, ref o,
ref o, ref o);
//Console.WriteLine(oDoc.Paragraphs.Count);
//Console.WriteLine(oDoc.Tables.Count);
foreach (Paragraph p in oDoc.Paragraphs)
{
Range rng = p.Range;
Style styl = rng.get_Style() as Style;
//Console.WriteLine(styl.NameLocal);
if ((bool)rng.get_Information(WdInformation.wdWithInTable)
== true)
{
foreach (Cell c in rng.Cells)
{
if (rng.Cells.Count > 0)
Console.WriteLine(c.Range.Text.ToString());
}
}
else
Console.WriteLine(rng.Text);
}

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

latheef

unread,
Aug 20, 2009, 3:19:01 AM8/20/09
to
Hi,

I need to get the table in the word doc just after the section heading which
matches the predefined string.
Please let me know the below things:

1) How do I get the section heading in the word document.
2) How do I get the table present after a particular section heading.

Thanks,
Latheef

Jerry Chen

unread,
Jul 20, 2010, 3:46:52 AM7/20/10
to
for reading table in c# in word document , there is a easiest way to do this,
Spire.Doc. it's very easy to reading and writing word document.

http://www.e-iceblue.com/Introduce/word-for-net-introduce.html

Hop help to you.

0 new messages