Erase bookmark when copy table from excel to word

A

Alex St-Pierre

Hi !
When I copy a table from excel to word, the cells(1,1) in word contains a
bookmark. Is there a possibility to copy the table without the excel bookmark
?
Dim rngExcel As Excel.Range
Set appExcel = GetObject(, "Excel.Application")
Set wbExcel = appExcel.Workbooks(fileExcel)
Set rngExcel = wbExcel.Application.Range("table1_1")
Set rng = docWord2.Tables(1).Range
Set tbl = rng.Tables(1)
tbl.Select
tbl.Delete
rngExcel.Copy
Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False,
RTF:=False

Thank you.
 
J

Jean-Guy Marcil

Alex St-Pierre was telling us:
Alex St-Pierre nous racontait que :
Hi !
When I copy a table from excel to word, the cells(1,1) in word
contains a bookmark. Is there a possibility to copy the table without
the excel bookmark ?
Dim rngExcel As Excel.Range
Set appExcel = GetObject(, "Excel.Application")
Set wbExcel = appExcel.Workbooks(fileExcel)
Set rngExcel = wbExcel.Application.Range("table1_1")
Set rng = docWord2.Tables(1).Range
Set tbl = rng.Tables(1)
tbl.Select
tbl.Delete
rngExcel.Copy
Selection.PasteExcelTable LinkedToExcel:=False,
WordFormatting:=False, RTF:=False


The bookmark is a reference to the Excel cell range.
To delete it, try something like this:

Dim appExcel As Excel.Application
Dim wbExcel As Excel.Workbook
Dim rngExcel As Excel.Range

Dim docWord2 As Word.Document
Dim rng As Word.Range
Dim tbl As Word.Table

Set appExcel = GetObject(, "Excel.Application")
Set wbExcel = appExcel.Workbooks("Book1.xls")
Set rngExcel = wbExcel.Application.Range("table1_1")

rngExcel.Copy

Set docWord2 = ActiveDocument
Set rng = docWord2.Tables(1).Range

With rng
.Tables(1).Delete
.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
.Bookmarks(1).Delete
End With

BTW, no need to set a Table object, select it and then delete it. See how I
did it in my version of your code.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Alex St-Pierre

Thanks Jean-Guy!!
It works good..
The reason why I used tbl.Select was because after deleting the table, the
cursor was going anywhere in the document..! By using your code, the new
table always replace the existing table.
I had "Set tbl = rng.Tables(1)" at the end because I used the table property
to format line style, column width, etc. thereafter
 

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