locked
Copy only tables from one document to another RRS feed

  • Question

  •  How to copy only tables ? This is not working "originalDoc.tables(10).Copy "

    Thanks in Adavance

    • Split by Cindy Meister MVP Saturday, February 22, 2014 2:30 PM Split for visibility from http://social.msdn.microsoft.com/Forums/office/en-US/717506ed-16e9-4cdb-b5b7-46c9715b04bc/how-to-use-word-vba-to-copy-the-content-of-one-file-to-another?forum=worddev#939f4107-64bc-4ba2-b8b3-a648441bf64b
    Friday, February 21, 2014 9:39 PM

All replies

  • Try

    originalDoc.Tables(10).Range.Copy


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, February 21, 2014 10:15 PM
  • Thanks originalDoc.Tables(10).Range.Copy is working . 

    I need one more help .

    1)I have 25 tables in worddoc 1 , 25 tables in worddocdoc2 .

    2)copy 25 + 25 tables to worddoc3

    3)Need to sort the tables based on SL.No column (Column A in each table is having SL.No)

    Saturday, February 22, 2014 10:28 AM
  • Do you mean that you want to sort each table separately on the first column?

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Saturday, February 22, 2014 10:43 AM
  • Hello Hans, Yes i want to sort and then consolidate the documents .

    I did it. But dont know this is right or not . 

    1)Initially i am getting tableID's,Col A content of worddoc1, worddoc2 and then storing it and doing sort  in excel sheet.  

    2)To merge it i did little changes to your above code. 

    'Note::This will run in Excel

    Sub Merging() Dim originalDoc1 As Word.Application, tempDoc1 As Word.Application, newDoc1 As Word.Application Dim originalDoc As Word.Document, tempDoc As Word.Document, newDoc As Word.Document Dim myPath As String, myPath1 As String, myPath2 As String, myPath3 As String Dim rng As Word.Range myPath3 = "\creat.docx" myPath4 = "\creat1.docx" 'Read Excel sheet contains SlNO/TableID and Docpath details Set sh = ThisWorkbook.Sheets("Merge") lastrow = sh.Cells(Rows.Count, "A").End(xlUp).Row 'Open the document to consolidate and copy the sorted table Set newDoc1 = CreateObject("Word.Application") 'Set newDoc = Documents.Add Set newDoc = newDoc1.Documents.Open(myPath3) newDoc1.Visible = True With ActiveDocument.PageSetup .Orientation = wdOrientLandscape End With 'Copy tables 1 by 1 based on the sort info available in Excel For t = 2 To lastrow TID = sh.Range("B" & t).Value myPath1 = sh.Range("C" & t).Value Set originalDoc1 = CreateObject("Word.Application") Set originalDoc = Documents.Open(myPath1) originalDoc1.Visible = False originalDoc.Tables(TID).Range.Copy Set rng = newDoc.Content rng.Collapse Direction:=wdCollapseEnd rng.PasteAndFormat (wdFormatOriginalFormatting) originalDoc.Close SaveChanges:=False Next t newDoc.SaveAs myPath4 newDoc.Close SaveChanges:=False MsgBox ("Merging Done !!!") Kill_Word End Sub




    Saturday, February 22, 2014 2:47 PM
  • You don't have to create multiple Word.Application objects - you only need to create one such object at the beginning of the macro, use that to open, manipulate and close documents, then quit it at the end of the macro.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, February 23, 2014 12:36 AM