# Copy Multiple Excel Tables to Word



## Fanel (Jan 16, 2019)

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!

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


----------



## Fanel (Jan 16, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Attach the Excel


----------



## Macropod (Jan 16, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*



Fanel said:


> Attach the Excel


You cannot attach workbooks here. See *Posting Aids*: https://www.mrexcel.com/forum/board-announcements/127080-guidelines-forum-use.html


----------



## Macropod (Jan 16, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Try something along the lines of:

```
Sub ExportExcelDataToWordDocument()
Dim wdApp As New Word.Application, wdDoc As Word.Document, i As Long
Const StrFl As String = "C:\Users\stefan.georgescu\Desktop\Fisa"
With wdApp
  'Hide the Word session
  .Visible = False
  ' create a new Word Document based on the specified template
  Set wdDoc = .Documents.Add(StrFl & ".dotm")
  With wdDoc
    'loop through sheets 2 to 6 and copy them to the corresponding Word bookmarks
    For i = 2 To 6
      If .Bookmarks.Exists("bookmark" & i) Then
        'copy the whole worksheet to clipboard.
        Sheets(i).UsedRange.Copy
        'paste from the clipboard to the Word Doc.
        .Bookmarks("bookmark" & i).Range.Paste
      End If
    Next
    'Save Word Document
    .SaveAs2 StrFl & ".docx"
    'Close Document
    .Close False
  End With
  'exit Word
  .Quit
End With
'MEMORY CLEANUP
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
```


----------



## DanteAmor (Jan 16, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Use the following. 

You have to put which table goes in which brand in the section "set tables and marks"



```
Sub ExportExcelDataToWordDocument2()


     Application.ScreenUpdating = False
    'Dim wdExcelApp As Application               'Excel is the default library (optional)
    Dim wdWordApp As Word.Application   'Word app
    ' Creating a new instance of Word
    Set wdWordApp = New Word.Application 'instantiate a new instance of Word 2010
    '
    ' Set tables and marks
    tablas = Array("tabla1", "tabla2", "tabla3")    'name of your tables
    marcas = Array("bookmark1", "bookmark2", "bookmark3") 'corresponding with the tables above
    '
    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"
        
        sheet2.Activate
        For i = LBound(tablas) To UBound(tablas)
            Range(tablas(i)).Copy
            .Selection.GoTo what:=-1, Name:=marcas(i) '"bookmark2" ' -1 means "wdgotobookmark"
            .Selection.Paste      'paste from the clipboard to the Word Doc.
        Next
        
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Save WORD Document
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim TheFileName As String
        TheFileName = "C:\Users\stefan.georgescu\Desktop\Fisa.docx"
        '
        '(SaveAs is for Office 2003 and earlier - deprecated)
        .ActiveDocument.SaveAs 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
End Sub
```


----------



## Fanel (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Thanks


----------



## Fanel (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

@macroPROD & DanteAmor
I tried both macros but they are not what I want  @macroprod: Your macro loops and bring tables from al the sheets in my excel, and I dont want this.
in my case I just want to brig multiple tables that are located only in "Sheet2", I want to set a specific word above each table (e.g Chapter I.1 and the table below to copy to Wd Bookmark1; Chapter II.1 and the table below to copy to Wd Bookmark2; Chapter III.1 > Bookmark3.....) and based on that word to copy the below table into designated Bookmark in Wd. 
Hope that I make myself clear. Sorry for my bad English. 
Please help me!


----------



## Macropod (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

In that case, you need to explain how your 'tables' - as you referred to them in your initial post - are defined. Your:
Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy
doesn't appear to relate to any particular table.


----------



## Fanel (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

In my initial macro, I had just one table in "Sheet2" and that table was copyied in Wd Bookmark2, now I need from the same sheet to bring multiple tables (I think there will be at least 48 tables in "Sheet2"), and each of them to copy in specific Bookmark in Wd. I dont know haw to adapt the macro to my criteria.
 P.S. I will try to put an image of the Ex Sheet2.


----------



## Fanel (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*






[/URL] free photo hosting[/IMG]


----------



## Fanel (Jan 16, 2019)

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!

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


----------



## Fanel (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

I want to search the criteria above each table and bring the table to Wd (e.g.Table below II.1 copy to Wd Bookmark1; Table below III.1 copy to Wd Bookmark2.....). Your Help will be highly appreciated! Thanks!


----------



## DanteAmor (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

To name a table: select the range from "A1" to "F16" and in the name box type, for example: "tabla1", then select the range from "A18" to "F33" and in the name box type "tabla2" and so on with each mark.


In the macro in these lines you must relate each table with its respective mark

```
tablas = Array("tabla1", "tabla2", "tabla3")    'name of your tables
    marcas = Array("bookmark1", "bookmark2", "bookmark3") 'corresponding with the tables above
```


Execute the macro.


----------



## Macropod (Jan 17, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*



Fanel said:


> I want to search the criteria above each table and bring the table to Wd (e.g.Table below II.1 copy to Wd Bookmark1; Table below III.1 copy to Wd Bookmark2.....). Your Help will be highly appreciated! Thanks!


You still haven't said anything to indicate how a macro would recognise what range any given table spans. If a given table only ever spans a range like, say, A1:J10, you need to say so. Alternatively, as DanetAmor suggests, you could name the range each table spans.


----------



## Fanel (Jan 18, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

@Dante: the macro work fine, but I don't want the tables to be an exact range (A1"F16). 
In my initial post I had a line of code in which I wanted the tables to be *Dynamic* (I think this was the line of code - Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy).
Pls help!


----------



## Fanel (Jan 18, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

@macroprod: I want all the tables from Sheet2 to be Dynamic, that's why I wanted to search for a specific word above each table (II.1; III.1; III.2...), and then to bring the table below but in Dynamic mode. Could you help me with it, please?Thanks much!


----------



## Fanel (Jan 18, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*



Fanel said:


> @Dante: the macro work fine, but I don't want the tables to be an exact range (A1"F16).
> In my initial post I had a line of code in which I wanted the tables to be *Dynamic* (I think this was the line of code - Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy).
> Pls help!



I tried to insert the following lines of code to search in first table Dynamic, begin from A2 and stop where it finds last value in first table, and then I want same thing for next tables:
    Worksheets("Sheet2").Activate
    Range("A2", Range("A2").End(xlDown).End(xlToRight)).Copy
    Set mydoc = wdApp.Documents.Open
    Set rngStart = ActiveDocument.Bookmarks("Bookmark1").Range

I have error 424 object required on this line: Set mydoc = wdApp.Documents.Open
Something is missing and I don't understand what?!


----------



## DanteAmor (Jan 18, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*



Fanel said:


> @Dante: the macro work fine, but I don't want the tables to be an exact range (A1"F16).
> In my initial post I had a line of code in which I wanted the tables to be *Dynamic* (I think this was the line of code - Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy).
> Pls help!




A table is dynamic, only you add or remove rows and automatically the range of the table is adjusted, then the macro only has to refer to the name of the table and not have to calculate each of the ranges. Try for a while with this dynamic, you will see that it will help you.


----------



## Macropod (Jan 18, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*



Fanel said:


> macroprod: I want all the tables from Sheet2 to be Dynamic, that's why I wanted to search for a specific word above each table (II.1; III.1; III.2...), and then to bring the table below but in Dynamic mode.


Your insistence on making the code work out for itself where the tables are by finding the 'specific word above each table' makes this much harder than it needs to be. You should be using suitably-named tables. I'm not going to waste time coding around your reluctance to do that.


----------



## Fanel (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Ok, thanks for your help!


----------



## Fanel (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Dante, thank you very much for helping me!
I followed your indications, but I didn't managed to make the ranges in each table to be Dynamic (the source to be Dynamic), so I can add/erase new data or rows and when I run the macro, all the data to be exported to Word. 
Please, is there a setting that I can make? Tks


----------



## Fanel (Jan 16, 2019)

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!

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


----------



## Macropod (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

The default behaviour for tables in Excel is to expand automatically if you add a new row at the bottom or a new column on the right; they'll also expand automatically if you add a new row or column within the existing range. See: https://www.contextures.com/xlExcelTable01.html Accordingly, if you create and use one appropriately, there won't be any issues regarding the range adjusting to take account of rows/columns being added/deleted.

PS: If you want a macro to process your tables, don't number them with Roman numerals like you've done in post 10.


----------



## Fanel (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

I renamed the tables, they are not numerals now. I was trying to make the tables expand (search until it finds blank box) with OFFSET formula from NameManager, but when I run the macro it doesen't bring me the selected tables in Wd.


----------



## Fanel (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Now my tables in Excel looks like this
	

	
	
		
		

		
		
	


	








https://ibb.co/ckKzxs6
In NameManager for first table (example name Gestiune SSC) the formula is =Sheet2!$A$2:$F$22
Now I want the formula tot search where it finds text in that range and that info to transfer to Wd (no blanks).
I made a test with OFFSET formula, but the macro doesn't want to bring any info to Wd.
Thanks


----------



## Macropod (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

The macro won't 'work' as it wasn't written for tables, as such, and we don't know what you've named them. As for trying to make them expand via the Offset formula, that suggests you're using named ranges, not table names. The two are entirely different. Read the link I posted.


----------



## Fanel (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Yes, I've used "name ranges" based on the macro provided by Dante in initial post. Now my macro looks like this.

```
Sub ExportExcelDataToWordDocument2()



     Application.ScreenUpdating = False
    'Dim wdExcelApp As Application               'Excel is the default library (optional)
    Dim wdWordApp As Word.Application   'Word app
    ' Creating a new instance of Word
    Set wdWordApp = New Word.Application 'instantiate a new instance of Word 2010
    '
    ' Set tables and marks
    tablas = Array("GestiuneSSC", "AlimATM", "Depridangajati")     'name of your tables
    marcas = Array("bookmark1", "bookmark2", "bookmark3") 'corresponding with the tables above
    '
    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"
        
        Sheet2.Activate
        For i = LBound(tablas) To UBound(tablas)
            Range(tablas(i)).Copy
            .Selection.GoTo what:=-1, Name:=marcas(i) '"bookmark2" ' -1 means "wdgotobookmark"
            .Selection.Paste      'paste from the clipboard to the Word Doc.
        Next
        
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Save WORD Document
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim TheFileName As String
        TheFileName = "C:\Users\stefan.georgescu\Desktop\Fisa.docx"
        '
        '(SaveAs is for Office 2003 and earlier - deprecated)
        .ActiveDocument.SaveAs 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
End Sub
```


----------



## Macropod (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

So, do you want your ranges to expand automatically using table names, or are you going to stick with named ranges?


----------



## Fanel (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Doesn't matter, until the source tables from Sheet2 (tables Gestiune SSC; Alim ATM; Dep rid angajati...) are dynamic and each time I modify them (in excel) to be imported accordingly in Word (specific bookmarks). 
But, from what I understand, if I stick with "named ranges" (how I managed to do the macro), probably they wont be dynamic, or I don't know how to make them dynamic. 
Then, please, if you have the time to help me and write the code so I can have the tables source dynamic, I will be grateful!


----------



## DanteAmor (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*



DanteAmor said:


> To name a table: select the range from "A1" to "F16" and in the name box type, for example: "tabla1", then select the range from "A18" to "F33" and in the name box type "tabla2" and so on with each mark.
> 
> 
> In the macro in these lines you must relate each table with its respective mark
> ...






I apologize for all the confusion that I caused.



I apologize for all the confusion that I caused.
Correct, to create tables, select the range of cells from A1 to F16, select from the Insert menu, Table, OK, in the table name field, type "GestiuneSSC", select the next range, select from the Insert menu, Table, Accept, in the table name field, type "AlimATM" and so on with each table.

Try the macro again


----------



## Macropod (Jan 21, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Assuming the tables/named ranges you want to copy are now named Table2 to Table6, only a few small changes to my original code are needed:

```
Sub ExportExcelDataToWordDocument()
Dim wdApp As New Word.Application, wdDoc As Word.Document, i As Long
Const StrFl As String = "C:\Users\stefan.georgescu\Desktop\Fisa"
With wdApp
  'Hide the Word session
  .Visible = False
  ' create a new Word Document based on the specified template
  Set wdDoc = .Documents.Add(StrFl & ".dotm")
  With wdDoc
    'loop through tables 2 to 6 and copy them to the corresponding Word bookmarks
    For i = 2 To 6
      If .Bookmarks.Exists("bookmark" & i) Then
        'copy the table to clipboard.
        ThisWorkbook.Sheets("Sheet2").Range("Table" & i).Copy
        'paste from the clipboard to the Word Doc.
        .Bookmarks("bookmark" & i).Range.Paste
      End If
    Next
    'Save Word Document
    .SaveAs2 StrFl & ".docx"
    'Close Document
    .Close False
  End With
  'exit Word
  .Quit
End With
'MEMORY CLEANUP
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
```
As you can see, only one line of code and two lines of comments have changed.


----------



## Fanel (Jan 22, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*






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!





https://ibb.co/y6gdRL2


----------



## Fanel (Jan 16, 2019)

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!

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


----------



## Fanel (Jan 22, 2019)

*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.


----------



## Fanel (Jan 22, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*



Fanel said:


> 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.
> ...



And now I see another issue, imports also the empty cells (see img)
https://ibb.co/JqMtnZV


----------



## Fanel (Jan 22, 2019)

*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!


----------



## Macropod (Jan 22, 2019)

*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.


----------



## Fanel (Jan 22, 2019)

*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


----------



## DanteAmor (Jan 22, 2019)

*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.


----------



## Fanel (Jan 23, 2019)

*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]


----------



## Macropod (Jan 23, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Cross-posted at: http://www.vbaexpress.com/forum/showthread.php?64486-VBA-Copy-tables-from-excel-to-Wd without any acknowledgement of the help you've already been given here.
Please read Mr Excel's policy on Cross-Posting in rule 13: http://www.mrexcel.com/forum/board-announcements/99490-forum-rules.html


----------



## Macropod (Jan 25, 2019)

*Re: Export multiple Tables from Excel to a Word document using VBA*

Also cross-posted at: https://www.excelforum.com/excel-pr...1261338-copy-tables-from-excel-to-wd-vba.html
Evidently you don't care about the cross-posting rules here or on any other forum. Thread closed.


----------

