Inserting docs based on desired order

S

Steve C

I've created a basic Word 2003 template with an opening form where users can
select any (or all) of four existing Word documents (DocA, DocB, DocC, DocD)
they want to insert into a resulting document. They use check boxes to
select what docs they want to insert, and can also indicate (using text
boxes) how many copies of each they want inserted. My existing code works
well when they click OK.

However, some users wish to insert the docs in a different order of their
choice, such as two copies of DocD first, then one of DocC, three of DocA and
perhaps none of DocB. So I created corresponding text boxes next to each Doc
in the form where they can enter the order they want to have them inserted.

My question is how to write code that handles all of the scenarios for the
order that they decide upon. For example, in the above scenario, I could
write an elaborate IF statement that compares every order number value to all
the others, but that seems like overkill. I'm thinking that a Select Case
statement would probably work best, but I need some general ideas on how to
structure it. Any help is much appreciated!
 
J

Jean-Guy Marcil

Steve C said:
I've created a basic Word 2003 template with an opening form where users can
select any (or all) of four existing Word documents (DocA, DocB, DocC, DocD)
they want to insert into a resulting document. They use check boxes to
select what docs they want to insert, and can also indicate (using text
boxes) how many copies of each they want inserted. My existing code works
well when they click OK.

However, some users wish to insert the docs in a different order of their
choice, such as two copies of DocD first, then one of DocC, three of DocA and
perhaps none of DocB. So I created corresponding text boxes next to each Doc
in the form where they can enter the order they want to have them inserted.

My question is how to write code that handles all of the scenarios for the
order that they decide upon. For example, in the above scenario, I could
write an elaborate IF statement that compares every order number value to all
the others, but that seems like overkill. I'm thinking that a Select Case
statement would probably work best, but I need some general ideas on how to
structure it. Any help is much appreciated!

I would do it this way:

1) On the userform itself, to avoid errors such as "insert 0 copies of DocB
after DocC" or users states number of copies but does not specify an
insertion order...
On four rows on the Userform:
DropDown for Insertion Order (Values -- to 4) All four dropdowns could have
a an tag property value, like Insert1 to Insert4 > Label with document name
(Tags Doc1 to Doc4) > Textbox for copy quantities (Tags Copy1 to Copy4).
When appearing on screen, the Insertion DropDown would be set to "--" *and*
disabled. All textboxes would be set to "0". The only wayt o enable the
dropdown for a document would be to enter a value other than "0" in its
corresponding Textboxes. If the user resets the copy value to 0, disable the
corresponding dropdown...

2) When user clicks on OK, check for errors... Like, did they forget to set
an Insertion dropdown (Any of the dropdown with "Enabled" status still set to
"--"?); or, do two or more of the insertion dropdowns have the same value? Do
all of the enabled dropdowns have increasing values from 1 to x? (If all
dropddowns are enabled, you know you need to have 1 to 4, if you do not find
a 1, there is an error, or, if you find 1, but no 2, there is an error...)
Etc.
If there are any errors, notify user and let him/her make corrections.

3) If you get to this stage, you know everything is OK. Create a sub that
will take three parameters: Name of document, a Range (Wherer to insert said
doc) and a Long indicating how many times it need to be inserted. Make sure
the Range is ByRef so that as you modify it when inserting the document, the
insertion point is always updated. Or, if you prefer, you can declare a
public Range object and use that instead with a two-parameter sub. Or, I
guess you could just assume that you always add the document at the end of
the current document, so a Range object is not all that necessary...

4) When leaving the userform, before unloading it, scan all the controls on
the userform, if a control has a certain tag value (i.e. contains "Insert")
and is enabled, check if the value is 1 (this could be a Long counter). Pick
up the name of the document associated with this dropdown control and the
Copy quantities (If you picked up a dropdown control with the tag "Insert3,"
you know you need to get "Doc3" and "Copy3") . Call the sub passing it the
appropriate info. Increase the counter to two. Loop again until you find the
dropdown equals to two. If there aren't any, you are done...
This counter maximum value could be set by the error checking routine. As it
checks the values of the four dropdowns, you could establish how many
doucments need to be inserted. While you are at it, you could create a
collection... When you find a dropdown with a value of 1, the first item in
the collection would be the corresponding document, and so on. So when you
get to point 4) above, you have a collection with oducment names already
sorted, all you need to do at that stage is pick up the number of copies
before calling the sub that does the work of inserting the document...

Also, I guess that instead of Tags, you could use the controls' names
instead... I like tags because sometimes I can give them all the same value,
control names have to be unique.

So many options!
 
S

Steve C

Jean-Guy:

Thank you so much for your extensive reply. You've given me some great food
for thought, and I will use your ideas to move forward. I'll let you know
what I come up with!
 

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