VBA Macro to fill a Dynamic Array of Strings with strings being pulled out from the first cell of every table in a word document

In a Word document I have a number of tablea. Each Table has 2 columns and multiple rows.
In the first cell of each table (ie in the first column of the first row) other than the first two tables, I have the full name of a person (made up of a first name and a last name).
I would like to have a macro that would pull out the first name (from the full name) of every table in my word document (with the exception of the first two tables) and put it into an array of strings (called clientNames in my code). So after the macro runs my clientNames array should have the first names of all clients (having pulled them out from the first cell of every table in word document (with the exception of the first 2 tables.
The first two tables do not have any names in the first cell but have some different type of data, that is why I want to ignore them.
I have tried doing this with the code in the code window, but I keep getting either an error (subscript out of range) or my clientNames array does not get filled.
I would appreciate your help.
Private Sub cmdPullOutFirstNameNames()

Dim firstRow As range
Dim ClientFirstAndLasName As String
Dim FullName() As String
Dim FirstName As String
Dim i As Integer
Dim tableCount As Integer
tableCount = ActiveDocument.Tables.Count
tableCount = tableCount - 2

Dim clientNames() As Variant
i = 3

For i = 3 To ActiveDocument.Tables.Count
Set firstRow = ActiveDocument.Tables(i).Rows(1).range
ClientFirstAndLasName = firstRow.Cells(1).range.Text
ClientFirstAndLasName = Replace(ClientFirstAndLasName, Chr(7), "")
FullName = Split(ClientFirstAndLasName, " ")
FirstName = FullName(0)

ReDim clientNames(tableCount)
' in the above line if just hard code the number of tables such as clientNames(20) the code then does not fill clientNames with the names.
 clientNames(i) = FirstName
Next i

' if my code was working correctly I would expect something like below to happen
'clientNames(0) = "Catherine"
'clientNames(1) = "Peter"
'clientNames(2) = "Robert"
'clientNames(3) = "Smith"
' and so forth 


End Sub

Open in new window

LVL 1
FaheemAhmadGulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Hi Faheem,

Please try this and see if that works fine for you.
Private Sub cmdPullOutFirstNameNames()
Dim doc As Document
Dim firstRow As Range
Dim ClientFirstAndLasName As String
Dim FullName() As String
Dim FirstName As String
Dim i As Integer, cnt As Integer
Dim clientNames() As Variant

i = 3
Set doc = ActiveDocument
If doc.Tables.Count < 3 Then
    MsgBox "Not enough tables in the ActiveDocument.", vbExclamation
    Exit Sub
End If

For i = 3 To doc.Tables.Count
    cnt = cnt + 1
    ReDim Preserve clientNames(1 To cnt)
    Set firstRow = doc.Tables(i).Rows(1).Range
    ClientFirstAndLasName = firstRow.Cells(1).Range.Text
    ClientFirstAndLasName = Replace(ClientFirstAndLasName, Chr(7), "")
    FullName = Split(ClientFirstAndLasName, " ")
    FirstName = FullName(0)
    clientNames(cnt) = FirstName
Next i

'Check if array was populated successfully
For i = 1 To UBound(clientNames)
    MsgBox clientNames(i)
Next i
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
Here's a slightly different take.
Option Explicit

Private Sub cmdPullOutFirstNameNames()
Dim firstRow As Range
Dim ClientFirstAndLasName As String
Dim FullName() As String
Dim FirstName As String
Dim i As Long
Dim tableCount As Long
Dim clientNames() As Variant

    tableCount = ActiveDocument.Tables.Count - 2
    
    ReDim clientNames(1 To tableCount)

    For i = 3 To ActiveDocument.Tables.Count

        Set firstRow = ActiveDocument.Tables(i).Rows(1).Range
        ClientFirstAndLasName = firstRow.Cells(1).Range.Text
        ClientFirstAndLasName = Replace(ClientFirstAndLasName, Chr(7), "")
        FullName = Split(ClientFirstAndLasName, " ")
        FirstName = FullName(0)
    
        clientNames(i - 2) = FirstName
    
    Next i

End Sub

Open in new window

FaheemAhmadGulAuthor Commented:
Many thanks both Neeraj and Norie.
Both solutions worked perfectly and added to my understanding and therefore I am accepting both as correct. Hope this OK.
Regards

Faheem
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome Faheem! Glad we could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.