Consulting

Results 1 to 14 of 14

Thread: Solved: copy from excel to word unmerged table

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    Solved: copy from excel to word unmerged table

    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?

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

    [/vba]
    I try also this (no success):

    [vba]
    wordDoc.Tables(wordDoc.Tables.Count).Select
    Selection.MergeCells = False
    [/vba]

  2. #2
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    I putted like this:


    [vba]
    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
    [/vba]
    First will autofit the columns, copy then will paste the table.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    [VBA]
    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

    [/VBA]

  4. #4
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    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)

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    You have to adapt the path and filename of course......
    [VBA]
    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

    [/VBA]

  6. #6
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    ok. What I am doing wrong (via Bookmarks already created in an created word file):

    [vba]

    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
    [/vba]

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    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 ?
    Last edited by snb; 03-05-2013 at 07:51 AM.

  8. #8
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    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?

  9. #9
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    or

    [vba]
    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
    [/vba]
    but it is not working

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Please do some closereading of my suggestions first.

  11. #11
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    but double backslashes ? can be "tricked" with:

    ThisWorkbook.FullName

    because I do not have any result:

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

  12. #12
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    [vba]
    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
    [/vba]

  13. #13
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    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.

  14. #14
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •