PDA

View Full Version : Letter writing template



simoninman
10-05-2007, 01:10 PM
Hi all, I am wanting to paste around 20 letter templates into word and make a form that will come up when the file is opened and the user can select which template to choose from, it will then ask them for the name of the recipient and paste that onto the letter and then... save the one letter, close the template file and open the new letter ready.

i am just wondering if anyone could help me with this, i mainly need help for the form to select which letter, then select the name, and save just the one page.

thanks for your help.
Simon

lucas
10-05-2007, 01:54 PM
For the best insight I have found on this click here (http://vbaexpress.com/forum/showthread.php?t=13543).
see post#11 specifically and download the file in that post.

fumei
10-08-2007, 01:00 AM
Hi all, I am wanting to paste around 20 letter templates into word and make a form that will come up when the file is opened and the user can select which template to choose fromYou could do this. A userform that the user selects to call another specified template.

But really, there is no need to make so many other files. ESPECIALLY if you are talking about one-pager letters.

Make one file - a template file - display a userform asking which type of letter...then create that letter style. You can have user input - recipient, addresses...whatever - also on your userform. Grab that and fit your information into the letter...any of them. You can have ALL of the letter types in one file. The user selects what type they want, and the template clones itself as THAT letter type, by removing all the others.

simoninman
10-12-2007, 06:21 AM
thanks but how would the userform know where to place the information that has been entered?
also what kind of code could i use to clone the file?

thanks

fumei
10-12-2007, 08:06 AM
You clone a file using a template. that is what they do, and what they are for.

How would the userform know???? Ummm, userforms do not "know" anything, ever. You tell them to do things.

Did you look at the demo? Remember though that the demo I posted was a document, not a template.

Essentially, it works like this:

1. the template file (a .DOT) file has ALL of the contents, ALL of the letters.

2. each letter is individually bookmarked.

3. when the template is cloned (ie. used via File > New and selecting the template), its Document_New event fires a userform.

4. the userform handles everything.

What does that mean?

It means, first of all - and the demo shows this - the users selects what type of letter. Say the types of letters are: Financial, Staffing, Invoice, Purchasing...whatever. They are ALL in the original template (.dot file). They are bookmarked.


Bookmarks are named ranges. So the bookmark "Financial" covers the Financial letter. "Staffing" covers the Staffing letter.

The user selects Staffing from a dropdown on the userform.

The userform now removes all the other bookmarks. What is left? The Staffing letter.

So that takes care of that. Viola, you have a Staffing letter.

The user selects Financial....the userform removes all the other bookmarks, leaving just the Financial bookmark. Voila, you have a Financial letter.

OK so far?

Now the second part is you want to take user input, and place it in specific locations in whatever letter was selected.

There are two main methods for doing this. Well first of all, you would either change the userform to show the input fields for the specific letter; OR show a different userform with the input fields for the specific letter.

I am partial to changing the userform. I do not care for multiple userforms, but there are valid reasons for going that route. This would depend on how many fields you require.

In any case, you now have a userform with the input fields appropriate for the selected letter.

Getting data from a userform to specific locations in a document can be broken into two general choices.

Formfields, or Bookmarks.

If the user may change the information placed into the document, then formfields are probably the best route. Although this is debatable. Formfields are for user input.

If the information picked up from the userform input will not be changed - in other words, you are getting the information and "writing" it into the document, and basically that is that, then Bookmarks are, IMO, better.

The point being is that YOU, as the developer, tell the userform where to put the information.

Say one input is Client Name (there will likely be others, but we will just talk about one). So you have a textbox on the userform, named txtClientName. The user enters the name. And the others, whatever they are.

They click OK, or Finish, or Done button...or whatever name you want to use for starting the work.

That button starts the work.

In the example:ActiveDocument.Formfields("ClientName").Result = _
txtClientNameWhatever the user entered into the textbox (txtClientName) on the userform will be placed into the formfield ClientName.

Done.

But, you do not use code to clone the template. Oh, well you COULD have, say a button on a toolbar, that would use code to do it. Or have a shortcut key that starts it. Or a menu item that starts it.

All depends on how you want to start the process.

lucas
10-12-2007, 08:21 AM
I just noticed that you had posted to this thread Gerry. I had been working on your multiletter.dot example that you had posted in an earlier thread and if all they want to add from the userform is the address then it is fairly easy.......

I attached it so you can tell me if it has any serious problems that you can see...hope you don't mind what I have done with your original document Gerry

As Gerry noted earlier this is a .doc file for evaluation....do not save the changes each time you try it and when you get it looking the way you want it save it as a .dot or template file.

fumei
10-12-2007, 09:54 AM
Heck Steve, once something is posted it is public domain. Do whatever you want.

I notice that you have Selection typing the value from the address textbox. As it stands, that is OK, as the selection is at the top of the document - the usual place for an address.

However, to make it a wee bit better I have done the following.

1. inserted an Address bookmark at the top of the document. Note: I had to delete the Letter1 bookmark first, then put it back in again. This because the original Letter1 bookmark covered Range(0, 0) - the start of the document. In other words, even though I could PUT an Address bookmark at the start of the document, selecting Letter2 removes Letter1...which incuded the Address bookmark.

2. changed the TabOrder on the userform. I use the keyboard a lot, so I wanted to Tab from entering something in the Address textbox directly to the OK button. In other words:

type address
Tab
Enter - which fires the OK button.

Having an independent Address bookmark required a little change to the code. If the bookmark is NOT the one selected, AND is NOT "Address"...then delete it.

It also required a procedure to fill the bookmark. This is in the standard module, not the userform module.Sub FillBookmark(strBM As String, strText As String)
ActiveDocument.Bookmarks(strBM).Select
Selection.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, _
Range:=Selection.Range
End Subwhich called from the commandbutton procedure. For Each ctl In fraChoices.Controls
If ctl = True Then
For Each objBookmark In ActiveDocument.Bookmarks()
If objBookmark.Name <> _
Right(ctl.Name, Len(ctl.Name) - 3) Then
If objBookmark.Name <> "Address" Then
objBookmark.Range.Delete
End If
End If
Next
Exit For
End If
Next
Call FillBookmark("Address", TextBox1.Text)

lucas
10-12-2007, 01:04 PM
I understand completely Gerry and I like it. Bookmarks are the easiest for me to use in word. I dislike formfields. For one thing I don't understand them completely(my own fault) but they don't seem as user friendly as a userform to me.

It would be even harder to add more data(date, etc.) than that to a multi-page template like this...but I can see where it could be done....

fumei
10-12-2007, 01:18 PM
I like userforms because I can control what goes into the document. They allow the use of on-going error trapping.

However, there are some very good uses for formfields.

In fact, I sometimes use them and remove the shading so the fields are invisible to the user (but the contents are not - usually text), do NOT allow user input (they are disabled).

This essentially can act like a kind of bookmark, as they are kinds of bookmarks.

Done like that, you do not need the FillBookmark procedure to put data in, or out, or change. The formfield .Result works fine.