Move down under Word table

henrik2h

Board Regular
Joined
Aug 25, 2008
Messages
155
Office Version
  1. 2021
Platform
  1. Windows
I have an Excel macro that should take a range in Excel and paste that and merge with a table in Word. For that to work I need to place the cursor just below the Word table and paste.

Question: how do I get to the position just below the table in Word from Excel VBA? (I also tried with bookmarks, hence the redudant code in there, but I need to expand the table two times and that is not working, the second time ends up in the middle of the word table.

VBA Code:
Sub Export_data()

    Dim wb As Workbook
    Dim wApp As Word.Application
    Dim wDoc As Object
    Dim wFileSelect As String
    Dim tbl_rng As Range
    Dim tblS_rng As Range
    Dim wRng As Word.Range
    Dim WordTable As Word.Table
    
    Application.ScreenUpdating = False
    
    Set wApp = CreateObject("Word.Application")
    
    'Open the target Word file
    
    wFileSelect = Application.GetOpenFilename("Word-files,*.doc*", _
        1, "Select Source File To Open", , False)
    'if the user didn't select a file, exit sub
    If TypeName(wFileSelect) = "Boolean" Then Exit Sub
    
    Documents.Open wFileSelect
    Set wDoc = ActiveDocument
        
    wApp.Visible = True
        
    'Copy table from Excel to Word
    
    wb.Activate
    Set tbl_rng = wb.Worksheets("Lgh").ListObjects("tbLghlista_Output").DataBodyRange
    tbl_rng.Copy

    'Paste Table into MS Word
    'With wDoc.Bookmarks("BM_Lghlista1")
    '.Select
    '.Range.PasteAppendTable
    'End With
    
    THIS SECTION IS MY PROBLEM
    wDoc.Activate
    wDoc.Tables(1).Select
    Selection.Collapse WdCollapseDirection.wdCollapseEnd
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Range.PasteAppendTable
    
    'Repeat for Totals row
    'Set tblS_rng = wb.Worksheets("Lgh").ListObjects("tbLghlista_Output").TotalsRowRange
    'tblS_rng.Copy
    
    'With wDoc.Bookmarks("BM_Lghlista2")
    '.Select
    '.Range.PasteAppendTable
    'End With

    
    
    
    'Autofit Table so it fits inside Word Document
    Set WordTable = wDoc.Tables(1)
    WordTable.AutoFitBehavior (wdAutoFitContent)
 
    CutCopyMode = False
    Set wApp = Nothing
    Set wDoc = Nothing
            
    Application.ScreenUpdating = True


    
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

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