Copy table to word vba but will not open word .

danpan

New Member
Joined
Aug 27, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi all so I've managed to make this code work for what i needed it to , it opens word , it opens and pastes the table in the desired position on my device.
when I copy the code into my work computer i keep getting a runtime error and it cant open the word document.
any suggestions or any improvements that you can see that i could make to the code would be greatly appreciated. To give you an idea of my level at excel back in April I had no idea how to create a table.
Any help is greatly appreciated.

The code is courtesy of sigma code. with minimal adjustments.

VBA Code:
Sub TblWrd()

'declare word Variables
Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim WrdTbl As Word.Table
Dim wrksht As Worksheet
Dim wrdrange As Word.Range
Dim wrdshp As Word.InlineShape

'declare excel variables
Dim Exceltbl As ListObject

'create  a new istance of word
Set WrdApp = New Word.Application
    WrdApp.Visible = True
    WrdApp.Activate '<-  I know the issue is at that line  and I know googling will give me the actual answer but id rather undertand what the problem is and why it fails to open world ,[/COLOR][/B]
 
    'open document in word application
    Set WrdDoc = WrdApp.Documents.Open("C:\......112.dotx")

    'loop through the list objects in the applicaton
    For Each Exceltbl In ActiveSheet

   'Set Exceltbl = ActiveSheet.ListObjects("TABLE1", "table2", "table3")
    Exceltbl.Range.Copy
  
   'pause the excel application for one second
   Application.Wait Now() + #12:00:02 AM#
  
  'paste to bookmark
  Set wrdrange = WrdDoc.Bookmarks(1).Range   
    
     'paste to word
            With wrdrange
        .PasteExcelTable linkedtoexcel:=True, WordFormatting:=True, RTF:=True
    End With
 
  'create a referemce to the table we just pasted
  Set WrdTbl = WrdDoc.Tables(WrdDoc.Tables.Count)
      WrdTbl.AllowAutoFit = True
      WrdTbl.AutoFitBehavior (wdAutoFitWindow)
     
      'create a new page
       WrdApp.ActiveDocument.Sections.Add
       WrdApp.Selection.GoTo what:=wdGoToPage, which:=wdGoToNext
      
       'clear my clipboard
       Application.CutCopyMode = False
      
       Next
    Next

End Sub
 
Last edited by a moderator:

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,514
if I wanted to specify the order in which the tables get pasted ,I would assume the best way of doing so is naming the tables , and assigning a bookmark to each of them . ?The sections part looking back at it , I was probably trying to see if I could paste each table to a new section .But I think the bookmark maybe the easies way.
Just to give some context Im trying to create a document where most cells are mail merged into word from excel and then in certain areas of the form to add the respective table if that makes sense
In an ordinary 'letter' mailmerge, each 'letter' is output to a separate Section in the output document, so there is no need to create those in code. Conversely, bookmarks don't survive a mailmerge, so you'd need to find another way of telling Word where to do the pasting. The simplest approach to that might be to insert a basic table into your Word mailmerge main document with just the heading row, then paste your Excel tables to that using Word's PasteAppendTable method. For example, see my answer in: vba - Transfer data from Excel to pre-existing Word table - Stack Overflow.

As for ordering the tables, you'd need to have a way for the loop to associate each table with the corresponding merged record in the mailmerge output document. Alternatively, you might try combining the mailmerge with a DATABASE field, which might obviate the need for a macro. However, since you haven't told us anything about the structure of the workbook used for the mailmerge and, in particular, the tables you want to output, it's impossible to say which approach is best.
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Watch MrExcel Video

Forum statistics

Threads
1,123,281
Messages
5,600,720
Members
414,401
Latest member
grenona2020

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
Top