Unable to add table

S

Sanya Ibrahim

Hello all,
I am trying to add a table to a new word 2007 document i created through an
excel macro. I wanted to add the data from the excel sheet to this table.
After creating the document, i added some text through
selction.TypeText("text") and added some paragraph marks through
selection.TypeParagraph methods. I tried to add the table through the
following code:-

word.selection.EndKey mode:=wdStory, Extend:=wdMove
Dim tble as Word.Table
set tble = Word.Selection.Tables.add(word.range,2,8)

On execution of the last line, all previously entered text is deleted
from the document and the table is added in its place.

Can any one tell me, how to overcome this issue? Should I post it in
the Excel Macro section?

Thanks and regards,
Sanya
 
G

Graham Mayor

Set tble = Word.Selection.Tables.Add(Word.Selection.Range, 2, 8)
should do the trick.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Fumei2 via OfficeKB.com

Just to clarify why...

set tble = Word.Selection.Tables.add(word.range,2,8)

This uses the range of the whole document as the range to use for the new
table. Thus, your previously inserted text is deleted.

Set tble = Word.Selection.Tables.Add(Word.Selection.Range, 2, 8)

This uses the current selection (at the end of the document) as the range to
use for the new table. Thus the table is added to the Selection range, NOT
the whole document.

Graham said:
Set tble = Word.Selection.Tables.Add(Word.Selection.Range, 2, 8)
should do the trick.
Hello all,
I am trying to add a table to a new word 2007 document i created through
[quoted text clipped - 17 lines]
Thanks and regards,
Sanya
 
S

Sanya Ibrahim

Thank you all very much. both of your suggestions resolved my problem.

Sanya

Fumei2 via OfficeKB.com said:
Just to clarify why...

set tble = Word.Selection.Tables.add(word.range,2,8)

This uses the range of the whole document as the range to use for the new
table. Thus, your previously inserted text is deleted.

Set tble = Word.Selection.Tables.Add(Word.Selection.Range, 2, 8)

This uses the current selection (at the end of the document) as the range to
use for the new table. Thus the table is added to the Selection range, NOT
the whole document.

Graham said:
Set tble = Word.Selection.Tables.Add(Word.Selection.Range, 2, 8)
should do the trick.
Hello all,
I am trying to add a table to a new word 2007 document i created through
[quoted text clipped - 17 lines]
Thanks and regards,
Sanya

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201003/1

.
 

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