Fix this vba script to allow to paste multiple tables from Excel Sheets to Template word doc

ShavarK

New Member
Joined
Jan 18, 2017
Messages
3
Currently this macro only paste the second table into the sheet, so it basically overwrites the first table and paste the second table. I need it to paste ALL tables as you see in code, how do i fix this?
Code:
Sub[COLOR=#303336] [/COLOR][COLOR=#2B91AF]ExportToWord[/COLOR][COLOR=#303336]()[/COLOR]<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">[COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Worksheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Val Balance Sheet"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#2B91AF]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"A1:D22"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#2B91AF]Copy[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Dim[/COLOR][COLOR=#303336] wdapp[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] wddoc [/COLOR][COLOR=#2B91AF]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Object[/COLOR][COLOR=#303336]

 [/COLOR][COLOR=#2B91AF]Dim[/COLOR][COLOR=#303336] strdocname [/COLOR][COLOR=#2B91AF]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]String[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]On[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Error[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Resume[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Next[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wdapp [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]GetObject[/COLOR][COLOR=#303336](,[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"Word.Application"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Err[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]429[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Then[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Err[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Clear[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wdapp [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]CreateObject[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Word.Application"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]End[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336]

 wdapp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Visible[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]True[/COLOR][COLOR=#303336]
 strdocname [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"C:\Users\ako\report.docx"[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Dir[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]strdocname[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]""[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Then[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#2B91AF]MsgBox[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"The file "[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] strdocname [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] vbCrLf [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"was not found "[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] vbCrLf [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"C:\Users\ako\."[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] vbExclamation[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"The document does not exist."[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Exit[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Sub[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]End[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336]

 wdapp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Activate[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wddoc [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] wdapp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]documents[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Open[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]strdocname[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336] wddoc [/COLOR][COLOR=#2B91AF]Is[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Nothing[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Then[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wddoc [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] wdapp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]DocumentOpen[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]strdocnme[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 wddoc[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Activate[/COLOR][COLOR=#303336]
 wddoc[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Range[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Paste[/COLOR][COLOR=#303336]


 [/COLOR][COLOR=#2B91AF]Worksheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Template Gains and Losses"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#2B91AF]Range[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"A1:C11"[/COLOR][COLOR=#303336]).[/COLOR][COLOR=#2B91AF]Copy[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Dim[/COLOR][COLOR=#303336] wdapp2[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] wddoc2 [/COLOR][COLOR=#2B91AF]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Object[/COLOR][COLOR=#303336]

 [/COLOR][COLOR=#2B91AF]Dim[/COLOR][COLOR=#303336] strdocname2 [/COLOR][COLOR=#2B91AF]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]String[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]On[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Error[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Resume[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Next[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wdapp2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]GetObject[/COLOR][COLOR=#303336](,[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"Word.Application"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Err[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Number[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]429[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Then[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Err[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Clear[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wdapp2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]CreateObject[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Word.Application"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]End[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336]

 wdapp2[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Visible[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]True[/COLOR][COLOR=#303336]
 strdocname2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"C:\Users\ako\report.docx"[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Dir[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]strdocname2[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]""[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Then[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]MsgBox[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"The file "[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] strdocname2 [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] vbCrLf [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"was not found "[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] vbCrLf [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"C:\Users\ako\."[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] vbExclamation[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"The document does not exist."[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Exit[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Sub[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]End[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336]

 wdapp2[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Activate[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wddoc2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] wdapp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]documents[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Open[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]strdocname[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]If[/COLOR][COLOR=#303336] wddoc2 [/COLOR][COLOR=#2B91AF]Is[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Nothing[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Then[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wddoc2 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] wdapp2[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]DocumentOpen[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]strdocnme2[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
 wddoc2[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Activate[/COLOR][COLOR=#303336]
 wddoc2[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Range[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Paste[/COLOR][COLOR=#303336]


 wddoc[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Save[/COLOR][COLOR=#303336]
 wdapp[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Quit[/COLOR][COLOR=#303336]

 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wddoc [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Nothing[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Set[/COLOR][COLOR=#303336] wdapp [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Nothing[/COLOR][COLOR=#303336]
 [/COLOR][COLOR=#2B91AF]Application[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]CutCopyMode[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]False[/COLOR][COLOR=#303336]

[/COLOR]</code>[COLOR=#303336] [/COLOR][COLOR=#2B91AF]End[/COLOR][COLOR=#303336] [/COLOR][COLOR=#2B91AF]Sub[/COLOR]
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,216,471
Messages
6,130,822
Members
449,595
Latest member
jhester2010

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