Copy Multiple Excel Tables to Word

Status
Not open for further replies.

Fanel

New Member
Joined
Nov 8, 2018
Messages
27
Please help!
I am new to VBA, and I need help to make the following macro to import from XL ("Sheet2 ") multiple tables 1,2,3..., import them to a specific Wd doc Bookmarks 1, 2, 3, each table from sheet2 linked to one Bookmark from Wd doc.
Based on the macro attached I managed to bring only the first table from Sheet2 and copy to designated spot Bookmark2. Now I need to bring the next table (e.g. III.1 table below) an copy-it to Wd Bookmark3, and so on.
One way, maybe set macro to search the specific text above table (e.g. Chapter III.1), and import only the table below that text.
NOTE: In Wd doc "Fisa.docx" I have infomation that I dont want to be erased when the tables are imported from Exl. Thank you!
Code:
[/COLOR]Sub ExportExcelDataToWordDocument2()



    'Dim wdExcelApp As Application               'Excel is the default library (optional)
    Dim wdWordApp As Word.Application   'Word app
  
  Application.ScreenUpdating = False
  
' Creating a new instance of Word
    Set wdWordApp = New Word.Application 'instantiate a new instance of Word 2010
  
  
    With wdWordApp
      
        ' Making Word Visible on the screen
        .Visible = True             'iff false, document is invisible.
        .Activate ' make it the top pane, bring it to the front.
        


      
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' create a new Word Document based on the specified template
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        .Documents.Add "C:\Users\stefan.georgescu\Desktop\Fisa.dotm"
      
        'as before, copy the whole table from sheet to clipboard.
        Sheet2.Activate
        Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy
      
        .Selection.GoTo what:=-1, Name:="bookmark2" ' -1 means "wdgotobookmark"
        .Selection.Paste      'paste from the clipboard to the Word Doc.
        
  
      
      
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Save WORD Document
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim TheFileName As String
        TheFileName = "C:\Users\stefan.georgescu\Desktop\Fisa.docx"
          
        '(SaveAs is for Office 2003 and earlier - deprecated)
        .ActiveDocument.SaveAs2 TheFileName
            'replaces existing .doc iff exists
          
          
        ' Close Documents and Quit Word
        .ActiveDocument.Close 'close .DOCx
        .Quit 'exit Word
    End With
  
  Application.ScreenUpdating = True
  
    'MEMORY CLEANUP
    Set wdWordApp = Nothing 'garbage collection
    'Set wdExcelApp = Nothing 'OPTIONAL
  
  
End Sub
[COLOR=#2A2E2E]
[/IMG]
 
Re: Export multiple Tables from Excel to a Word document using VBA

Hi, Macro,
Thank you so much for all the time and support, I will try both macros, and start with the one indicated by Dante.
I will come back with feedback after I run also the macro you gave me.
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Re: Export multiple Tables from Excel to a Word document using VBA

y6gdRL2
Hello, Dante, and thank you again for all your support!
I've made the adjustment accordingly, the macro works but I have 2 issues:
1) In Wd it doesn't brings the Header of the table (see img URL);
2) I have one error in line "Range(tablas(i)).Copy", Run-time err. 1004 Method "Range of object"_Global failed.
Please help!
y6gdRL2
https://ibb.co/y6gdRL2

And now I see another issue, imports also the empty cells (see img)
https://ibb.co/JqMtnZV
 
Upvote 0
Re: Export multiple Tables from Excel to a Word document using VBA

Macro, I managed to run your macro, and I have same issues:
1. Tables with no headers;
2. Copies also the blank rows.
Please, tell me what to do!
Tks again guys, you are the best!
 
Upvote 0
Re: Export multiple Tables from Excel to a Word document using VBA

In that case, I suggest you check what ranges you've applied the names to.
 
Upvote 0
Re: Export multiple Tables from Excel to a Word document using VBA

No 1 issue: The tables are inserted according to indications made by Dante, from Insert>Table, but in the box (Refers to) where I have the range this is not active, I cannot change the range.
https://ibb.co/8Km0PQn
 
Last edited:
Upvote 0
Re: Export multiple Tables from Excel to a Word document using VBA

Directly in the table, you can take the lower right corner of the table and drag to the left and up to the row and column you want, or you can delete the table and create it again.
 
Upvote 0
Re: Export multiple Tables from Excel to a Word document using VBA

Tried both, doesn't work :(
Same two problems:
1. Tables with no headers;
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]2. Copies also the blank rows, if I have a table without information in cells I want to bring in Wd only the Header of table.[/FONT]
 
Last edited:
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top