Dima Dz Dima Dz - 2 months ago 7
MySQL Question

VBA MS Word content controls messed order

I have a table with about 15 content controls. The content controls have different titles.

Now, I copy-paste the table with content controls a couple of times, and later, get different values into every single content control from the database. Since the content controls from different tables share the same name, I thought of looping through number of tables using something like this

seqNo = 1
For Each t in MyTables
ActiveDocument.SelectContentControlsByTitle("title1").Item(seqNo).Range.Text = "some value 1 from DB"
ActiveDocument.SelectContentControlsByTitle("title2").Item(seqNo).Range.Text = "some value 2 from DB"
' and so on
seqNo = seqNo + 1

The problem is when I use this code, my content controls don't get filled in sequentially. I mean, for example, content control with title
from table1 isn't filled with its value, instead, content control with title
from table4 gets that value. And this mess goes around really bad: values from table 2 can end up in table 4, 9, 10 and so forth.

I think the order of content controls gets messed up somehow when I copy-paste the tables.

And clue how to get it right?


Didn't really find why this happens, but went with giving unique names to the content controls, like title1, title2, and so on, and then looping through all of them to set the needed values.