From: Sanya Ibrahim on
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
From: Graham Mayor on
Set tble = Word.Selection.Tables.Add(Word.Selection.Range, 2, 8)
should do the trick.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



"Sanya Ibrahim" <SanyaIbrahim(a)discussions.microsoft.com> wrote in message
news:8AB936A1-BF9F-4290-BB25-ED248C0F1A0B(a)microsoft.com...
> 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


From: Fumei2 via OfficeKB.com on
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 Mayor wrote:
>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

From: Sanya Ibrahim on
Thank you all very much. both of your suggestions resolved my problem.

Sanya

"Fumei2 via OfficeKB.com" wrote:

> 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 Mayor wrote:
> >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
>
> .
>