+ Reply to Thread
Results 1 to 2 of 2

VBA copy and paste tables into word

Hybrid View

  1. #1
    Registered User
    Join Date
    07-19-2013
    Location
    Scranton, Pa
    MS-Off Ver
    Excel 2013
    Posts
    14

    VBA copy and paste tables into word

    Hi, I am looking for some help modifying this code. Is there a way to apply this code to multiple tabs? I have a spreadsheet with 7 tabs. Each tab has its own table. I would like to paste each table into a Word document one after the other and so on.

    Thanks

    Sub ExcelRangeToWord()
    
    'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
    'NOTE: Must have Word Object Library Active in Order to Run _
      (VBE > Tools > References > Microsoft Word 12.0 Object Library)
    'SOURCE: www.TheSpreadsheetGuru.com
    
    Dim tbl As Excel.Range
    Dim WordApp As Word.Application
    Dim myDoc As Word.Document
    Dim WordTable As Word.Table
    
    'Optimize Code
      Application.ScreenUpdating = False
      Application.EnableEvents = False
    
    'Copy Range from Excel
      Set tbl = ThisWorkbook.Worksheets(Sheet1.Name).ListObjects("Table1").Range
    
    'Create an Instance of MS Word
      On Error Resume Next
        
        'Is MS Word already opened?
          Set WordApp = GetObject(class:="Word.Application")
        
        'Clear the error between errors
          Err.Clear
    
        'If MS Word is not already open then open MS Word
          If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
        
        'Handle if the Word Application is not found
          If Err.Number = 429 Then
            MsgBox "Microsoft Word could not be found, aborting."
            GoTo EndRoutine
          End If
    
      On Error GoTo 0
      
    'Make MS Word Visible and Active
      WordApp.Visible = True
      WordApp.Activate
        
    'Create a New Document
      Set myDoc = WordApp.Documents.Add
      
    'Copy Excel Table Range
      tbl.Copy
    
    'Paste Table into MS Word
      myDoc.Paragraphs(1).Range.PasteExcelTable _
        LinkedToExcel:=False, _
        WordFormatting:=False, _
        RTF:=False
    
    'Autofit Table so it fits inside Word Document
      Set WordTable = myDoc.Tables(1)
      WordTable.AutoFitBehavior (wdAutoFitWindow)
      
    EndRoutine:
    'Optimize Code
      Application.ScreenUpdating = True
      Application.EnableEvents = True
    
    'Clear The Clipboard
      Application.CutCopyMode = False
    
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: VBA copy and paste tables into word

    Try something like this (not tested).

    Sub ExcelRangeToWord()
        
        'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
        'NOTE: Must have Word Object Library Active in Order to Run _
         (VBE > Tools > References > Microsoft Word 12.0 Object Library)
        'SOURCE: www.TheSpreadsheetGuru.com
        
    '    Dim tbl       As Excel.Range
        Dim WordApp   As Word.Application
        Dim myDoc     As Word.Document
        Dim WordTable As Word.Table
        Dim ws        As Worksheet
        
        'Optimize Code
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        
    '    'Copy Range from Excel
    '      Set tbl = ThisWorkbook.Worksheets(Sheet1.Name).listobjects("Table1").Range
        
        'Create an Instance of MS Word
        On Error Resume Next
        
        'Is MS Word already opened?
        Set WordApp = GetObject(class:="Word.Application")
        
        'Clear the error between errors
        Err.Clear
        
        'If MS Word is not already open then open MS Word
        If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
        
        'Handle if the Word Application is not found
        If Err.Number = 429 Then
            MsgBox "Microsoft Word could not be found, aborting."
            GoTo EndRoutine
        End If
        
        On Error GoTo 0
        
        'Make MS Word Visible and Active
        WordApp.Visible = True
        WordApp.Activate
        
        'Create a New Document
        Set myDoc = WordApp.Documents.Add
        
        For Each ws In ThisWorkbook.Worksheets  'Loop through all worksheets
        
            If ws.listobjects.Count > 0 Then    'Test if worksheet has a table
            
                ws.listobjects(1).Range.Copy    'Copy first Table
        
                'Paste Table into MS Word (last Paragraph)
                myDoc.Paragraphs.Last.Range.PasteExcelTable _
                     LinkedToExcel:=False, _
                     WordFormatting:=False, _
                     RTF:=False
                    
                'Autofit Table so it fits inside Word Document
                myDoc.Tables(myDoc.Tables.Count).AutoFitBehavior wdAutoFitWindow
                
                'Add paragreaph below each table
                myDoc.Paragraphs.Add myDoc.Paragraphs.Last.Range
        
            End If
        
        Next ws
        
    EndRoutine:
        'Optimize Code
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        
        'Clear The Clipboard
        Application.CutCopyMode = False
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2014, 10:06 AM
  2. [SOLVED] Copy excel sheets to Word Tables
    By oneillp1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-25-2013, 12:00 PM
  3. Replies: 1
    Last Post: 01-16-2013, 05:36 AM
  4. Replies: 1
    Last Post: 10-01-2012, 11:11 PM
  5. Copy Word tables to Excel
    By Don in forum Excel General
    Replies: 3
    Last Post: 02-13-2005, 01:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1