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.
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