MS Word Tables from C#: Best way to get cursor out of table ...

B

BBFrost

I've successfully created a C# app (with a lot of help from these groups)
that extracts data from a database and then creates a table for each set
of data returned.

I removed the cursor from each table and before creating and adding
the next table to the document using the following incantation ...

(1) Extract 1st Data from Db ...
(2) Move to the "\\endofdoc" bookmark
(3) Create Ms Word Table ...
(4) Fill Table cells with Db data ...

(5: actual C# code used)
object oUnit = Word.WdUnits.wdStory;
object oMove = Word.WdMovementType.wdMove;
oWordApp.Selection.EndKey(ref oUnit, ref oMove);
oWordApp.Selection.InsertParagraphAfter();
oWordApp.Selection.EndKey(ref oUnit, ref oMove);

(6) Extract next set of Data from Db ...
(7) Move to the "\\endofdoc" bookmark
(8) Create Ms Word Table ...
(9) Fill Table cells with Db data ...

(10)
object oUnit = Word.WdUnits.wdStory;
object oMove = Word.WdMovementType.wdMove;
oWordApp.Selection.EndKey(ref oUnit, ref oMove);
oWordApp.Selection.InsertParagraphAfter();
oWordApp.Selection.EndKey(ref oUnit, ref oMove);

This all works fine as long as you start with a BLANK Doc and
each new table can start at the physical end of the Doc.

THE PROBLEM ...

Now I'm trying to call this table creation module from a template
document that has a bookmark that marks where the table should
be positioned in the MIDDLE of the document.

Ooooopppsss. Moving to the end of the document bookmark doesn't
work anymore.

The only method I can think of to fix this is to create 2 bookmarks
in the word template that will act as the upper and lower bounds of the
tables that are to be created.

Unfortunately I don't have enough understanding of the MS Office
Interop 10.0 library PIA's to figure out how, much less the best way, to ...

(1) move the cursor out of the table that has just been completed
(2) physically position the cursor past the end of the just created table
(maybe ?? position the cursor at a lower bound bookmark.)
(3) create another paragraph mark
(maybe ?? insert a paragraph BEFORE the lower bound bookmark.)
(4) move to that paragraph so that the next table I create won't be
automatically 'rolled into' the last table created.

If any of you knows how to do this or knows of an example that
illustrates how to do this I'd I really appreciate some help.

Thanks in advance !!

Barry Frost
Oregon

P.S. I've found the next 2 references to be reasonably up to date with the
10.0 library and the 3rd
reference to cover a number of very handy techniques.
The
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto200
3_ta/html/WordObject.asp
"Understanding the Word Object Model from a .Net Developer's Perspective"
and
http://support.microsoft.com/?kbid=316384 "How To: Automate word from Visual
C# . Net ..." have a
number of excellent examples but both tend to build a document from the top
down rather than try to
insert objects in the middle of an existing Template.
http://www.kayodeok.btinternet.co.uk/favorites/kbofficewordhowto.htm "Ms
Word How To examples"
 
B

BBFrost

Ok, I got it working.

I ended up creating an 'insertion' point bookmark foreach table bounded by
upper and
lower bookmarks that bounded the insert area for the tables.

I moved the cursor out of the table by moving the insertion point to the
lower bookmark
boundry after setting the table up and before filling the cells.

Barry Frost
Oregon
 

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