Linking UserForm through VBA programming

T

tarantula

sir/ma'am,
i have got a userform where i get the data enetered by the user
and store that data in a table created in a new WORD document. now i
have got another form wher the user enters another set of data but would
like to add it in the same table table (by appending additional columns
to the same table)of that document when he presses the 'SUBMIT' button
of that form.
the problem is that the table of that form is not visible in a
different user form.i have even tried to declare the table as a public
variable(object) in a new module but that doesn't work either.
Please help!!!

Thank you.
 
S

Simon Lloyd

tarantula;382510 said:
sir/ma'am,
i have got a userform where i get the data enetered by the user and
store that data in a table created in a new WORD document. now i have
got another form wher the user enters another set of data but would like
to add it in the same table table (by appending additional columns to
the same table)of that document when he presses the 'SUBMIT' button of
that form.
the problem is that the table of that form is not visible in a
different user form.i have even tried to declare the table as a public
variable(object) in a new module but that doesn't work either.
Please help!!!

Thank you.tarantula, welcome to The Code Cage, you need to post your current
userform code(s) so that we can help you with that.


--
Simon Lloyd

Regards,
Simon Lloyd
'The Code Cage' (http://www.thecodecage.com)
 
P

Pesach Shelnitz

Hi,

It seems to me from what you wrote that the first user form is creating a
new Word document and a table in it with code something like the following.

Dim doc As Document
Dim tbl As table

Set doc = Documents.Add(Template:=Environ$("AppData") _
& "\Microsoft\Templates\normal.dotm")
Set tbl = doc.Tables.Add(Selection.Range, _
NumRows:=1, NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior)

The second form uses the same Documents collection as the first form since
this collection is global to the instance of the Word application running on
the computer. However, in order for the second form to access the new doc
created by the first form, it would need to know the index of the new doc in
the Documents collection. From what I've seen, the Documents.Add method
creates the new file with the index 1 and renumbers the other open docs in
the collection. However, opening the second form would assign the index 1 to
it and change the index of the new doc to 2. So, if no other Word documents
are created or opened before the second form tries to add data to the second
table, it should be able to use the following code to add a new column to the
table.

With Documents(2).Tables(1)
.Columns.Add
End With

However, in my opinion, you can't rely on your users opening only the forms
and docs that you need them to open in order to ensure that the index of the
new doc will be 2. For this reason, you could add code to the second form to
cycle through all the open docs and identify the new doc by finding some
unique text or bookmark in it, or you could have the first form save and
close the new doc and then have the second form open it by name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top