Replace a word table by an excel table

A

Alex St-Pierre

Hi,
I have a table in Word and I replace it by a range table in excel. Excel
range ("table1_1") should go to replace the table in bookmark("table1_1"), ...
I use the following codification but the table #2 and #3 are copied inside
word table #1 (last cell) ?? When I delete the second table, it's like if the
cursur is going to table #1. Is there a way to use a replace table instead of
using delete and copy-paste function ?
Thanks !
--
Alex St-Pierre

For nbTab = 1 To 3

If nbTab = 1 Then
Set rngExcel = wbExcel.Application.sheets("table1.1").Range("table1_1")
Set rng = docWord1.Bookmarks("Table1_1").Range
ElseIf nbTab = 2 Then
Set rngExcel = wbExcel.Application.sheets("table1.2").Range("table1_2")
Set rng = docWord1.Bookmarks("Table1_2").Range
ElseIf nbTab = 3 Then
Set rngExcel = wbExcel.Application.sheets("table1.3").Range("table1_3")
Set rng = docWord1.Bookmarks("Table1_3").Range
End If

Set tbl = rng.Tables(1)
tbl.Delete
rngExcel.Copy
Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False,
RTF:=True
Set tbl = rng.Tables(1)
...
Next nbTab
 
E

Ed

Hi, Alex. I'm not an expert by any means, but let me make a suggestion:
Try replacing Selection in
Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False,
with tbl.Range to give
tbl.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False,

"Range" returns an area independent of the insertion point / cursor
position. "Selection" returns the exact insertion point, regardless of any
range you have referenced in your code. If your insertion point is in a
table cell, then Selection will paste everything there, and ignore any
referenced range.

HTH
Ed
 
A

Alex St-Pierre

I tried this but the newtable is always added to the old table
Set tbl = rng.Tables(1)
rngExcel.Copy
tbl.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False,
RTF:=True
Set tbl = rng.Tables(1)

This works:
Set tbl = rng.Tables(1)
rngExcel.Copy
tbl.select
selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False,
RTF:=True
 

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