Add Tables at a given Bookmark

J

Juan

Hi everyone,

I am trying to insert via C# a table at a given point in my template. The
problem is that it keeps inserting the table at the top of the document. I
have a bookmark where I want the table to be added, and still, it keeps
inserting the table at the top. If I try to insert a document, o type some
text, it finds the bookmark OK and inserts the document and types the text.
The table is still added at the top.

Any ideas???? Even if it you answers are for VB, doesn´t matter, translation
to C# is (normally) not that hard.

Thanks, from Spain

Juan Magaña
 
J

Jonathan West

Use the Add method of the Tables collection. This method includes a Range
parameter which defines where the table is inserted.

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup

Hi Juan,
 
J

Juan

Thanks, Jonathan,
that's what I´m doing, and probably that's what I´m doing wrong.
Before using the Add method, I select the bookmark where I want the table to
be placed (this works for typing text, adding a TOC or inserting a document,
but doesn´t work for adding a table).
Then, I use the add method, but the range I use is (0,0) and that's probably
why it places the table at the top of the document. How do I know what range
to use?
Thanks, from Spain
Juan Magaña
 
J

Juan

Nevermind, I figured it out. I don´t know if it´s the better way to do it,
but here it is:

- I find the bookmark in the document
- I select it
- I add the table, and as a range I use mySelection.Range...

Still, can I use a range without a bookmark? I mean, how do I know where to
insert a table if I don´t have a bookmark in the document?

Thanks, from Spain

Juan
 
J

Jonathan West

Juan said:
Nevermind, I figured it out. I don´t know if it´s the better way to do it,
but here it is:

- I find the bookmark in the document
- I select it
- I add the table, and as a range I use mySelection.Range...

Still, can I use a range without a bookmark? I mean, how do I know where to
insert a table if I don´t have a bookmark in the document?

Hi Juan,

You don't need to take the step of selecting the bookmark. You can set the
Range parameter directly using ActiveDocument.Bookmarks("MyBookmark").Range
 
B

Byron

Jonathan West said:
Use the Add method of the Tables collection. This method includes a Range
parameter which defines where the table is inserted.

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup

Hi Juan,

I'm having the same issue in Word XP. I want to insert a table
between a formfield and a bookmark. If I use the range of the
formfield for the insert, the field gets replaced by the table even
though the range is collapsed in the line before the insert. If I use
the range of the bookmark, the bookmark is then placed in the first
cell in the table. Is this a Word Bug?
 
J

Jay Freedman

Jonathan said:
Hi Juan,

You don't need to take the step of selecting the bookmark. You can
set the Range parameter directly using
ActiveDocument.Bookmarks("MyBookmark").Range

And to add the answer to your last question, you can use *any* range that
you can define. For example, you could use a Range object's .Find method to
locate some particular word or phrase, or some bit of formatting, to locate
the right spot.

For example, let's say your template contains a lot of static text
("boilerplate"), and somewhere in that text is a tag "[table here]". Your
macro could look like this:

Sub CreateTable()
Dim myRange As Range
Dim myTable As Table

Set myRange = ActiveDocument.Range
With myRange.Find
.Text = "[table here]"
.Replacement.Text = ""
If .Execute(Replace:=wdReplaceOne) Then
Set myTable = ActiveDocument.Tables _
.Add(Range:=myRange, _
NumRows:=2, NumColumns:=3)
End If
End With

If Not myTable Is Nothing Then
' do stuff
End If
End Sub

Instead of a tag, you can use a particular field, a paragraph formatted with
a unique style, a location such as the end of the document, or anything else
you can locate easily.
 

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