PDA

View Full Version : Solved: copy from excel to word unmerged table



white_flag
03-05-2013, 03:34 AM
hello

I try to copy paste an excel table to word.In excel I do not have merged cells. but wen I pasted in word some cells become merged (special cells from headers). What can I do to avoid this situation?


With ActiveWorkbook.Worksheets(shName)
.Range(Cells(rand, 1).Address, Cells(l_row, coloana).Address).Copy
wordDoc.Bookmarks("Tabel").Range.Paste
End With


I try also this (no success):


wordDoc.Tables(wordDoc.Tables.Count).Select
Selection.MergeCells = False

white_flag
03-05-2013, 04:01 AM
I putted like this:



With ActiveWorkbook.Worksheets(shName)
.Range(Cells(rand, 1).Address, Cells(l_row, coloana).Address).Columns.AutoFit
.Range(Cells(rand, 1).Address, Cells(l_row, coloana).Address).Copy
wordDoc.Bookmarks("Tabel").Range.Paste
End With

First will autofit the columns, copy then will paste the table.

snb
03-05-2013, 04:58 AM
Sub M_snb()
with createobject("Word.Document")
.Fields.Add Paragraphs(1).Range, , "includetext G:\\OF\\adressen.xls A1:K10", False
.Fields.Update
end with
End Sub

white_flag
03-05-2013, 05:31 AM
Hi snb

I will use .Columns.Autofit
then I will not gone have problems.
What do you think?


ps.Your solution is give me error 5825 (Object has been deleted)

snb
03-05-2013, 06:01 AM
You have to adapt the path and filename of course......

Sub M_snb()
With createobject("Word.Document")
.Fields.Add .Paragraphs(1).Range, , "includetext G:\\OF\\adressen.xls A1:K10", False
.Fields.Update
End With
End Sub

white_flag
03-05-2013, 07:15 AM
ok. What I am doing wrong (via Bookmarks already created in an created word file):



With ThisWorkbook.Worksheets(shName)
.Range(Cells(rand, 1).Address, Cells(l_row, coloana).Address).Columns.AutoFit
.Range(Cells(rand, 1).Address, Cells(l_row, coloana).Address).Copy
With wordDoc
.Fields.Add .Bookmarks("Tabel").Range, , ThisWorkbook.Path & "\" & ThisWorkbook.Name, False
.Fields.Update
End With
End With

snb
03-05-2013, 07:30 AM
You only need the code I provided.
Didn't you notice a Range was specified ? A1:K10
Didn't you notice the double backslashes ?
Are you familiar with Thisworkbook.fullname ?

white_flag
03-05-2013, 08:03 AM
notice that but all the time I had "Error! Not a valid filename." maybe because I have (. - in excel name)



so this: ThisWorkbook.Path & "\" & ThisWorkbook.Name ..etc will not gone go?

white_flag
03-05-2013, 08:08 AM
or


With ThisWorkbook.Worksheets(shName)
.Range(Cells(rand, 1).Address, Cells(l_row, coloana).Address).Columns.AutoFit
.Range(Cells(rand, 1).Address, Cells(l_row, coloana).Address).Copy
With wordDoc
.Fields.Add .Bookmarks("Tabel").Range, , "includetext" & ThisWorkbook.FullName & "A1:K10", False
.Fields.Update
End With
End With

but it is not working

snb
03-05-2013, 08:14 AM
Please do some closereading of my suggestions first.

white_flag
03-05-2013, 08:36 AM
but double backslashes ? can be "tricked" with:

ThisWorkbook.FullName

because I do not have any result:


With CreateObject("Word.Document")
.Fields.Add .Paragraphs(1).Range, , "includetext" & " " & ThisWorkbook.FullName & " " & "A1:K10", False
.Fields.Update
End With

snb
03-05-2013, 09:25 AM
Sub M_snb()
c01 = Replace(ThisWorkbook.FullName, ".xls", "_001.xls")
ThisWorkbook.SaveCopyAs c01

With CreateObject("Word.Document")
.Application.Visible = True
.Fields.Add .Paragraphs(1).Range, , "includetext " & c01 & " A1:K10", False
.Fields.Update
End With
End Sub

white_flag
03-06-2013, 02:10 AM
Good morning snb

Thank you for your solution.
but I will go via copy paste, because I have a "master sheet" that create the word document. and this master sheet is changing all the time.For revised document I will do: delete/keep existing table, insert new table.
to save a copy to "master sheet" is not an option.

mvg,
A.

Kenneth Hobs
03-06-2013, 10:25 AM
Here are two threads on the topic.

'Add Table to MSWord
' http://vbaexpress.com/forum/showthread.php?t=23975
' http://vbaexpress.com/forum/showthread.php?p=168731