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());
}
}
> 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 :-)
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.
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
http://www.e-iceblue.com/Introduce/word-for-net-introduce.html
Hop help to you.