Excel-VBA - Write Excel Range Data into Word Table and make calculation

Viewed 266

I am trying to bring this datarange in Excel over to a Table in Word including a simple multiplication calculation. code is from web source: https://www.groovypost.com/howto/insert-excel-table-into-word-with-vba/

For unknown reason the data do not appear in the Word table.

I am using a simple data table

Excel Data Input

And here is the Word table output Word table output

The Runtime error 91 (Object variable or With block variable not set) occurs at code line : strDate = tblRange.Cells(i + 1, j).Value

The strings strDate ,strItem, intUnits and intCost stay empty

See output local enter image description here

Any help - thanks

SOLVED I inserted Set tblRange = ThisWorkbook.Worksheets("Sheet1").Range("A1") immediately before For i = 1 To intNoOfRows and this resulted in a working code.

    Private Sub CommandButton1_Click()
    'First, create the variables and objects that’ll hold the data and allow you to write to the Word application.
    
    Dim tblRange As Excel.Range
    Dim WrdRange As Word.Range
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim WordTable As Word.Table
    Dim intRows
    Dim intColumns
    Dim strDate As String
    Dim strItem As String
    Dim intUnits As Variant
    Dim intCost As Variant
    Dim intTotal As Variant
    
    'Next, set the total columns and rows you want to read from the Excel range.       
    
    intNoOfRows = 10
    intNoOfColumns = 5
    
    'Repeat the same code as the last section that’ll open Word if it isn’t already open.
    
    On Error Resume Next
    Set WordApp = GetObject(Class:="Word.Application")
    **On Error GoTo 0**
    If WordApp Is Nothing Then Set WordApp = CreateObject(Class:="Word.Application")
    WordApp.Visible = True
    WordApp.Activate
    Set WordDoc = WordApp.Documents.Add
    
    'The next four lines create a table inside that newly opened Word document.
    
    Set WrdRange = WordDoc.Range(0, 0)
    WordDoc.Tables.Add WrdRange, intNoOfRows, intNoOfColumns
    Set WordTable = WordDoc.Tables(1)
    WordTable.Borders.Enable = True
    
    'Finally, the following loop will perform these actions:
    
    'For each row, put the order date, item, units, and cost into variables
    'Calculate unit times cost (total sale) and store that in a variable
    'For each column, write the values to the Word table, including the calculated total sale in the last cell
    'Move on to the next row, and repeat the procedure above
    'Here’s what that code looks like:
    Set tblRange = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    For i = 1 To intNoOfRows
        For j = 1 To intNoOfColumns
            If j = 1 Then
                strDate = tblRange.Cells(i + 1, j).Value
                strItem = tblRange.Cells(i + 1, j + 1).Value
                intUnits = Val(tblRange.Cells(i + 1, j + 2).Value)
                intCost = Val(tblRange.Cells(i + 1, j + 3).Value)
                intTotal = intUnits * intCost
            End If
    
    Select Case j
        Case Is = 1
            WordTable.Cell(i, j).Range.Text = strDate
        Case Is = 2
            WordTable.Cell(i, j).Range.Text = strItem
        Case Is = 3
            WordTable.Cell(i, j).Range.Text = intUnits
        Case Is = 4
            WordTable.Cell(i, j).Range.Text = intCost
        Case Is = 5
            WordTable.Cell(i, j).Range.Text = intTotal
        Case Else
    End Select
        Next
    Next
    
    End Sub
0 Answers
Related