Trying to make tables with macros in Word 2003 on XP

N

Nit_Wit_400

I'm sorta wet behind the ears when it comes to this kind of
programming, so I hope I don't confuse anyone and that I can explain
everything okay...

I'm trying to create a template in Word 2003 that is going to be used
as a handout in meetings.

So far, I've successfully used ASK fields to fill in the heading of
the handout (Meeting name, date, attendees, etc.).

Where I'm stuck is the variable amount of topics that could be in any
given meeting.

On the first page, there is a table listing each topic, the amount of
time (target time) each topic should take, and what time the topic
should start/end (i.e. if target is 15, time could be 12:15-12:30).

Populating that isn't too hard, I think the best way would be to use
text form fields and just let the meeting host fill them in to his
discretion -- except I don't know how to change the number of topics
and thus the number of text form fields to fill in or how to reference
each text form field after they've been created.

Each topic should have its own table and each table is identical
except for the topic name and target time. The rest is made up of
areas for notes and stuff like that.

How do I design this so that if there are say 5 topics, 5 tables are
created with a reference to each corresponding topic and target time?

Is there any way this can be done?

Thank you so much for your time.
 
N

Nit_Wit_400

I've had some ideas and I wanted to run them by the newsgroup:

Could I record making each table (up to 10) and use an ask field
asking "How many topics will be covered?" and then use those recorded
macros in conjunction with a reference to that ask field.

Example:

{IF{REF Topics} = "1" "Something" ""}
{IF{REF Topics} = "1" "Something" ""}
{IF{REF Topics} = "1" "Something" ""}

Where "Something" would be a macro creating the tables with the
references. Am I able to use a macro in this way? If so, how?
 
D

Doug Robbins - Word MVP

This suggestion is going to be a pretty steep (read almost "vertical")
learning curve for you, but I would create a template that contained a
userform that had textbox controls into which the user would enter the
Meeting name, date, attendees, etc. and another set of textbox controls into
which the user would enter a topic, the time and duration for that topic.
Then there would be a button that when clicked, added the data from this
second set of Textboxes to a Listbox on the form and finally a button that
when clicked would
take the information from the first set of textbox controls and insert it
into the required places in the document and then iterate through the data
records in the list box and create and populate a table in the document for
each of them.

I would also have buttons on the form that allowed the user to edit or
delete an item that they had added to the listbox before transferring
everything to the document.

If you want to give this a try, see
http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
N

Nit_Wit_400

Okay, so I've made a form, but I had some design questions... the
second set of text boxes you spoke of... I only need one set?

In other words, can the user make as many topics as he wants.. all he
has to do is enter each topic, time, and target time, one set at a
time, hit the button after each set, and that'll work?

I'm not so sure I understand how to implement that.. or how to create
tables after that's been accomplished.

If I could make this work, it would be WAY above and beyond what was
expected, and I haven't been able to produce those kinds of results
for a while. Thank you SO MUCH for pointing me in this direction.
 
D

Doug Robbins - Word MVP

The first set of text boxes was intended for getting information from the
user of things such as the subject of the meeting, the location, date, names
of attendees etc,

Dealing with the second set of text boxes which will be used for the agenda
items, if on your form, you create:

1. Three textbox controls to which you give the names (in the properties
dialog) of txtTopic, txtStart and txtDuration

2. A list box to which you give the name lstAgenda for which you set the
columnCount to 3, and set the ColumnWidths to appropriate values

3. A command button to which you give the name cmdAddItem and for which
you set the caption so that it reads "Add Item"

4. A command button to which you give the name cmdCreateAgenda and for
which you set the caption so that it reads "Create Agenda"

and you have the following code in the form for the Click events of the
command buttons, when you display the form, each time you enter data in the
three text boxes and then click on the Add Item button, the data from the
text boxes will be added to the list box and the text boxes will be cleared
ready for the data for the next agenda item. Then, when you have all of the
required information in the list box, if you click on the Create Agenda
button, the information will be transferred to the document. The code
assumes that in the document, you are starting with a one row table (in the
cells of which you would have the headings) to which rows will be added for
each of the agenda items.

Private Sub cmdAddItem_Click()
With lstAgenda
.AddItem txtTopic
.List(.ListCount - 1, 1) = txtStart
.List(.ListCount - 1, 2) = txtDuration
End With
txtTopic.Text = ""
txtStart.Text = ""
txtDuration.Text = ""
txtTopic.SetFocus
End Sub

Private Sub cmdCreateAgenda_Click()
Dim agenda As Table
Dim topic As Row
Dim TopicTable As Table
Dim rng As Range
Dim i As Long
Set agenda = ActiveDocument.Tables(1)
MsgBox lstAgenda.ListCount
For i = 1 To lstAgenda.ListCount
Set topic = agenda.Rows.Add
With topic
.Cells(1).Range.Text = lstAgenda.List(i - 1, 0)
.Cells(2).Range.Text = lstAgenda.List(i - 1, 1)
.Cells(3).Range.Text = lstAgenda.List(i - 1, 2)
End With
With ActiveDocument
.Range.InsertAfter vbCr
Set rng = .Range
rng.Collapse wdcollapsend
Set TopicTable = .Tables.Add(rng, 2, 2)
With TopicTable
.Cell(1, 1).Range.Text = lstAgenda.List(i - 1, 0)
.Cell(1, 2).Range.Text = lstAgenda.List(i - 1, 1)
.Rows(2).Cells.Merge
End With
End With
Next i
Me.hide

End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
N

Nit_Wit_400

Again, thank you soooo much for your help.

I'm almost there, I just need to work out one little kink that I can't
seem to get past.

When I'm creating the tables for each topic, they're going at the end
of the document -- which is how you coded it:

With ActiveDocument
.Range.InsertAfter vbCr
Set rng = .Range
rng.Collapse wdCollapseEnd
Set TopicTable = .Tables.Add(rng, 2, 2)
With TopicTable
.Cell(1, 1).Range.Text = LstTopics.List(i - 1, 0)
.Cell(1, 2).Range.Text = LstTopics.List(i - 1, 1)
.Rows(2).Cells.Merge
End With
End With

What I'm trying to do is set the range to the end of the second table
in the document, but I've been very unsuccessful. Let me give you an
example of what I've been trying.. maybe you can point me in the right
direction?

With ActiveDocumennt
Set rng = .Range(.Tables(2).Start,.Tables(2).End - 1)
rng.InsertAfter vbCr
Set TopicTable = .Tables.Add(rng, 3, 2)
With TopicTable
.Cell(1, 1).Range.Text = LstTopics.List(i - 1, 0)
.Cell(1, 3).Range.Text = LstTopics.List(i - 1, 1)
.Rows(2).Cells.Merge
End With
End With

......

I think (and I'm probably wrong) that tables don't work the same way
as paragraphs and I can't use them this way? If not, how can I set
the range to where I want in the document?



Side note:

I want to once again thank you for showing me how to do this, and
getting me started with vb. I'm growing fond of it. I have a
programming background, but the syntax of vb is pretty unfamiliar to
me and it took playing around with this project to get me started.
 
N

Nit_Wit_400

Instead of trying to do it this way.. I'm going to do a workaround
because I just now realized that if I try to make those tables in the
middle of the document (if I even can) instead of always putting them
on the end, I'll end up making them in reverse order and that wouldn't
be good.

I think what I'll do is create the "Next Steps" section of the handout
in code.. should be fun!

Thanks anyway!
 
D

Doug Robbins - Word MVP

The problem is that you are constantly referring back to the Range of the
second Table.

If you want the tables to be added in the middle of the document, the best
thing to do is insert a continuous Section Break at the point in the
document before which you want them to appear and use code such as:

Dim myrange As Range
Dim i As Long
ActiveDocument.UndoClear
For i = 1 To 3
Set myrange = ActiveDocument.Sections(1).Range
myrange.End = myrange.End - 2
myrange.InsertAfter vbCr '& vbCr
myrange.End = myrange.End - 1
myrange.Collapse wdCollapseEnd
ActiveDocument.Tables.Add myrange, 1, 1
Next i

Here I just used the i = 1 to 3 to set up a repetitive process to get it
functioning correctly in my tests. In your case that would be replaced by
the

For i = 1 To lstAgenda.ListCount

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 

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