PDA

View Full Version : Creating custom letters from VBA



mitchert
10-31-2008, 11:22 AM
Hello, all. My first post!

I want to write a VBA program in Word that creates form letters based upon a series of text and check boxes. The user would input some basic information (names, addresses, etc.) into text boxes and then would have a large number of check boxes from which to choose. Each check box would correspond to a paragraph of boilerplate language. (Ideally, each paragraph of boilerplate would be stored in a separate Word document from which the program could retrieve and copy the contents.) After typing in the info and checking the boxes, the user clicks a button and the program creates a new Word doc consisting of the user-inputted information and the contents of each file referenced by the check boxes.

Hopefully I've explained that clearly. Any help is greatly appreciated!

fumei
10-31-2008, 11:56 AM
"(Ideally, each paragraph of boilerplate would be stored in a separate Word document from which the program could retrieve and copy the contents.)"

Why "ideally"?????? In separate documents? That does not sound ideal to me. Why not AutoText entries in ONE document?

mitchert
10-31-2008, 12:05 PM
Ok. I've been playing around with AutoText and I see why you suggest it. So my initial question can be rephrased:

I want a VBA Word program that will:
(1) Let the user type custom information into text boxes
(2) Let the user select from a number of check boxes, each of which is associated with an AutoText entry
(3) On the push of a button, create a new Word doc that combines the user-inputted text with the AutoText entries associated with each check box.

I'm assuming this is a relatively simple task and there's existing code that would be a good starting point for me. I am welcome to any suggestions where I could find that code. I've tried looking but don't seem to have the proper search terms ... I'm not sure how to describe exactly what I want to do in concise enough terms for a search engine.

fumei
10-31-2008, 02:03 PM
Ok, you need to start by doing things step-by-step.

Make a userform that takes user entered text in a textbox, and puts that text into a document.

Add checkboxes.

Have the OK button (or whatever you want to call it) loop through the checkboxes. If the checkbox is checked, add the associated AutoText.

The coding is not extremely difficult. It is the logic that takes the work. For that I suggest actually writing it out, on paper. Put your chunks of text as boxes, and have arrows indicating the logic. For example, say you have three checkboxes, and two textboxes.

Three checkboxes that IF checked means "Insert AutoText X".

Two textboxes that MAY (or may not) have text.

YOUR logic must determine:

If textbox_A does have text in it....put it THERE.

IF textbox_A does have text in it, AND textbox_B does have text in it...put textbox_A contents THERE, followed by a paragraph mark, followed by textbox_B contents.

Or...maybe it is:

IF textbox_A does have text in it, AND textbox_B does have text in it...put textbox_A contents A DIFFERENT PLACE, and textbox_B contents THERE.

Who knows? What I am saying is that Word can do whatever you want, but YOU have to tell it exactly what that is. Which means YOU have to figure it out.

textbox_A is blank, textbox_B has text....what do you do? Do you insist that the user put something in textbox_A, and you will not do anything until they do? Does it matter?

What if textbox_A has text, and checkbox1 is checked? Does the AutoText associated with checkbox1 go BEFORE the text of textbox_A....after?

All this is logic.

If you can write that out clearly, and explicitly, then the actual coding is really not a big deal at all. We can help.

fumei
10-31-2008, 02:07 PM
Oh, and there is an alternative.

Rather than build UP a document by pieces, you can REMOVE pieces. This is, in fact, how I do multiple letter templates.

In this case, a template (.dot) file has all the letters. The choices the users makes on the userform (checkboxes, comboboxes, textbox contents) removes the parts of the template that are NOT relevant to those choices.

Steve (lucas) refers people to a thread where I posted an example, but darned if I know what that thread is. maybe he will read this and post a link.

lucas
11-01-2008, 10:07 AM
http://vbaexpress.com/forum/showthread.php?t=13543

Discussion starts at post #11 at the bottom of that post.

This is the best method I have ever been introduced to and I use it almost exclusively now. Thanks again to Gerry for introducing the idea.

It's almost as valuable as the FillABookmark code that Gerry also shared with us.

fumei
11-03-2008, 09:52 AM
Thanks Steve. I bookmarked it for myself now.

Mitchert, whether you use an additive method (building up chunks), or a reductive method (removing chunks) depends on each situation. I, like Steve, generally prefer having a reductive approach.

Why?

Because if everything is already there, in ONE place, it makes it easier to edit. If the template has, say, 9 letters inside it, and you need to change just one of those letters, you simply do that. You open the template and change the part that is the letter that needs changing.

Because it is inside a bookmark, you do not need to do anything else. The bookmark expands/contracts automatically. You can edit the portion of any given letter independently.

If you are using an additive method, and the added chunks are AutoText, there is no way you can edit the AutoText. You must delete it, and then re-create it (with whatever changes).

You can also use a combination of a reductive and additive approach.

lucas
11-03-2008, 10:23 AM
Attached is a demo that has three templates on it and the userform has two textboxes to input additional info into the document, no matter which template you choose using bookmarks and returns from the textboxes.

I have set this documents properties to read only. You can change that and save the document as a template(.dot) when you get it the way you want it.