+ Reply to Thread
Results 1 to 5 of 5

Paste a dynamic range into MS Word

Hybrid View

  1. #1
    Registered User
    Join Date
    02-13-2015
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    34

    Paste a dynamic range into MS Word

    Hi again,

    So I have the following code to paste a range from Excel to MS Word.


    Sub Synopsis()
    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("Data").Range("B4:C10")
      tbl.Copy
    
    '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.Open("template.docx")
      
    '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
    Currently the range is B4:C10 for testing, but I need the range to be dynamic in nature. For example, it will always start at B4 but could finish at C10, F10, H10, etc.

    Is there a way to alter the code to allow the range to start at B4 and end when there is no data left (at the bottom of the sheet)?

    Thanks in advance.
    Last edited by frostii; 12-24-2016 at 03:14 PM. Reason: renamed file within code

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,842

    Re: Paste a dynamic range into MS Word

    That's just a matter of find where the last column & row is and incorporating that into the range definition. For example:
    'Copy Range from Excel
      Dim r As Long, c As Long
      With ThisWorkbook.Worksheets("Data")
        With .UsedRange.Cells.SpecialCells(xlCellTypeLastCell)
          r = .Row
          c = .Column
        End With
        .Range(.Range("A1", .Cells(r, c)).Address).Copy
      End With
    Last edited by macropod; 12-24-2016 at 04:01 PM.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    02-13-2015
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    34

    Re: Paste a dynamic range into MS Word

    Thanks for the reply macropod!
    Please excuse my noobish-ness, but where in the code should I stick your version?
    Or, do I just replace the 'Copy Range from Excel' bit?
    Thanks again.

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,842

    Re: Paste a dynamic range into MS Word

    That code would replace either your first or second 'Copy Range from Excel' equivalent I'd probably go for the latter). You should delete the other 'Copy Range from Excel' instance, along with 'Dim tbl As Excel.Range', as that's no longer needed.

  5. #5
    Registered User
    Join Date
    02-13-2015
    Location
    Brisbane, Australia
    MS-Off Ver
    2016
    Posts
    34

    Re: Paste a dynamic range into MS Word

    I replaced the second 'Copy Range' part, removed the first 'Copy Range' part and everything is working beautifully.
    Thanks so much, will mark as SOLVED.

+ 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. Copy and paste in dynamic range
    By mangeshp4 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-28-2016, 07:31 AM
  2. [SOLVED] Copy and Paste Dynamic Range
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-27-2013, 11:14 AM
  3. Dynamic Range,Find last row & paste.
    By JapanDave in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2011, 01:19 AM
  4. Paste to a Dynamic range
    By abhitullu in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-14-2011, 08:10 AM
  5. Copy dynamic range from previous sheet and paste after last used row in a range
    By Mistweaver in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-04-2010, 06:24 PM
  6. Copy and paste dynamic range
    By wpryan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-16-2010, 09:42 AM
  7. Cut & paste dynamic range
    By bouncey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-10-2010, 05:44 PM

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