0
votes

I am working with VBA for the first time in a long time and need some help with adding a table into the document.

the line in question at the moment is

.Cell(1, x).Range.Select

and the error that I get is object variable or with block variable is not set. and for the life of me I cannot see where I am going wrong on this one.

I am passing in a 3 dimensional string array that I am looking to pass into a table and this is the code I am working with at the moment

Private Sub Create_Sized_Table_Multi_Column(i_Rows As Integer, i_Columns As Integer)
'create a table
Set t_newtable = ActiveDocument.Tables.Add(Selection.Range, i_Rows, i_Columns)
End Sub


Private Sub BuildTable(arr() As String, colCount As Integer, bookMark As String)

Dim t_newtable As Table
Dim i_Fund_Quantity As Integer
Dim i_Rows_Required As Integer
Dim i_Columns_Required As Integer
            'Number of funds is the upperbound + 1 to allow for the zero relative
            i_Fund_Quantity = UBound(arr) + 1
            'header Row
            i_Rows_Required = UBound(arr) + 1

            'Number of columns
            i_Columns_Required = colCount

'Add a table - this table will contain moved funds
'creates the table dimensioned by i_rows x i_column
            Create_Sized_Table_Multi_Column i_Rows_Required, i_Columns_Required

            'Now populate the header row
            With t_newtable
                For x = 0 To i_Columns_Required
                    .Cell(1, x).Range.Select
                    If x = 1 Then
                        Set_Table_Headers "Existing Fund"
                    ElseIf x = 2 Then
                        Set_Table_Headers "Customer Name"
                    ElseIf x = 3 Then
                        Set_Table_Headers "Switch To"
                        ActiveDocument.Bookmarks.Add ("bk_Switched_Table")
                    End If
                Next
            End With


            'Populate the table with the fund details
            ''//sp write to table here

            With t_newtable
                'Position cursor at first insertion point
                'ActiveDocument.Bookmarks("bk_Switched_Table").Select
                'Now create a loop
                For i_Loop_Rows = 0 To UBound(arr)

                        Set_Table_Rows
                        Selection.TypeText arr(i, 0)
                        Selection.MoveRight UNIT:=wdCell
                        Selection.TypeText arr(i, 1)
                        Selection.MoveRight UNIT:=wdCell
                        Selection.TypeText arr(i, 2)
                        t_newtable.Columns(3).Select
                        t_newtable.Columns.AutoFit
                        Selection.Collapse Direction:=wdCollapseEnd


                Next
            End With

            ActiveDocument.Bookmarks(bookMark).Select
            Selection.TypeParagraph
            Selection.TypeText s_Text
            Selection.TypeParagraph
            ActiveDocument.Bookmarks.Add (bookMark)

End Sub

I would be grateful if someone could review this and let me know where I have gone wrong and what I need to change.

thanks

Simon

2

2 Answers

1
votes

You never set t_newtable in your BuildTable sub. If you have to create your table somewhere else, then you have to

Set t_newtable = ActiveDocument.Tables(indexOfYourTable)

which will instantiate your t_newtable to the object you want it to be. NOTE: indexOfYourTable is 1 based not 0 based.

OR

You can put the line of code in your Create_Sized_Table_Multi_Column sub inside your BuildTable sub and pass the variables needed into your BuildTable sub.

1
votes

You declare t_newtable in the procedure Create_Sized_Table_Multi_Column, so it will be limited to that scope. If you want to call that procedure, have it create the table, then make it available to the code that called it, you need to change the Sub into a Function and have the function return the table.

For example:

Private Function Create_Sized_Table_Multi_Column(i_Rows As Integer, _
                                           i_Columns As Integer) As Table

  'create a table
  Set Create_Sized_Table_Multi_Column = ActiveDocument.Tables.Add( _
                                      Selection.Range, i_Rows, i_Columns)
End Function

Then you use it like this (code shortened for clarity):

Private Sub BuildTable(arr() As String, colCount As Integer, bookMark As String)

  Dim t_newtable As Table
  'Add a table - this table will contain moved funds
  'creates the table dimensioned by i_rows x i_column
  Set t_newtable = Create_Sized_Table_Multi_Column(i_Rows_Required, _
                                                i_Columns_Required)
End Sub

Note the added parenthesis around the call to the function. These are required when something is to be returned from a call.