Home > front end >  Userform populates wrong table in word document
Userform populates wrong table in word document

Time:08-05

I have been using google for a while and finally decided to ask here for help. Basically I have a word documents with a couple of tables that is being used by different people. Each table has a VBA button "add new row". Click on a button and a userform is filled out. Userform populates the table. Only problem left is there are multiple tables and userforms and they populate the wrong table.

How can I populate the right tables with userforms? Googling helped me that bookmarks are needed. I set a bookmark called

Table and Bookmark

Userform

Notation/Names

Code for clicking add in userforms:

Private Sub CommandButtonAdd_Click()
    Dim table2 As table
    Dim newRow As Row
    
    Set table2 = ActiveDocument.Tables(1)
    Set newRow = table2.Rows.Add
    
    newRow.Cells(1).Range.Text = TextBoxFunction.Text
    newRow.Cells(2).Range.Text = TextBoxName.Text
End Sub

First time using VBA, credits go to this person: https://www.youtube.com/watch?v=9cZ_XWzQZX0

Many many thanks for your help.

CodePudding user response:

You choose the table by index. If you have any mask you can recognize the correct table, you can try to check all the tables and recognize the right one with loop

Dim tempT as Variant
Dim count as long
count=0
For Each tempT in  ActiveDocument.Tables
count=count 1
if ActiveDocument.Tables(count) 'here some condition to recognize the corrct table ' Then 
Set newRow = table2.Rows.Add

'etc your code

End If 
Next tempT 
  • Related