Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with office word

Status
Not open for further replies.

associates

IS-IT--Management
Aug 2, 2005
59
0
0
AU
Hi,

I was wondering if anyone would be able to help me out. I'd like to set up a letterhead for the company i work for. I know that in office word, there is a wizard to set it up for us. However, this is what i would prefer it to do.

When you open up that letterhead.doc, a popup dialog box appears and prompt user for entering the company name, attention, addresses and date. Once those fields have been entered and the user click ok, those details are placed on theirs appropriate spot that has been pre determined before.

I think to do that, i need to write up a program. is that right?

Your help is greatly appreciated.

Thank you in advance and look forward to hearing from you.
 
This is straightforward and very do-able.

1. Do not use a letterhead.doc, use a letterhead.dot, that is, a template. Calling the template (NOT opening it) makes a clone of the original. This keeps the original intact. The clone is an exact duplicate and contains all macros of the original.

2. Create a UserForm with all the fields you need - company name, address, dates etc etc. When the user clicks OK, the UserForm takes the information entered and put is into the document.

There are a number of way to do this. You could have the information go into bookmarks, or you could have them go into formfields, or you could simply have the information go in a text - although you would probably use bookmarks to locate the spots to put the text.

Here is a SIMPLE indication of what you can do. We are going to use the default names of things - just to make it easy. I strongly recommend that in the future you do NOT use default names.

1. make a new document and type:
Company:

2. Right beside Company, insert a text formfield. Use the Forms toolbar (View > Toolbar > Forms). Move your mouse around until you see it indicate a Text Formfield. Click it. A shaded rectangle appears. This is a text formfield.

3. press enter to get a new line.

4. type Address: , and then insert another text formfield.

That is all for now. Use Save As to save the file as a Word template (.dot). Call it - oh...test1.dot.

Press Alt-F11 to get the Visual Basic Editor. Press Ctrl-R to get the Project Explorer. Locate in the project tree Project (test1), and highlight it (select it).

From the menu bar select Insert > Userform. Under Project (Test1) a new item should appear "Forms". Click the little plus sign to expand it, and then double click the item UserForm1.

Select View > Toolbox so you can see the toolbox. There are a number of items on it. Move the mouse around until you find Textbox. Select it and then draw on the UserForm a box. For now it does not really matter if it is pretty. Click the textbox icon on the toolbox again, and then draw another box. Now find the Commandbutton on the toolbox. Select it and draw a button on the userform.

Press F4 - this gives the Properties window. The current control should be the commandbutton, and you may as well change the caption. In the Properties window check that the name is CommandButton1. Then change the Caption (highlight it and type in) "OK". Without the quotes - unless of course you want the quotes...

In the Project Explorer highlight the userform (UserForm1), and then press F7. This opens the code module for the form. Copy and paste the following:
Code:
Private Sub CommandButton1_Click()
If TextBox1.Text <> "" And TextBox2.Text <> "" Then
    ActiveDocument.FormFields("Text1").Result = _
    TextBox1.Text
    ActiveDocument.FormFields("Text2").Result = _
    TextBox2.Text
    Unload Me
Else
    If TextBox1.Text <> "" Then
        MsgBox "Second textbox is blank"
    Else
        MsgBox "First textbox is blank"
        Exit Sub
    End If
End If
End Sub

Back in the Project Explorer, under Project (Test1) double click the ThisDocument item below it. Copy and paste the following into the code module that appears.

Sub Document_New()
UserForm1.Show
End Sub

Close the Visual basic Editor - use the top right Close "X".

Save the file (test1.dot).

Use the menu File > New and select test1.dot as the template. It will make a new document (a clone) and the UserForm will display immediately and will not close until you fill in both fields. The OK button will put the information into the formfields.

This is VERY VERY basic. It has minumal error trapping for the user input. Hopefully though it can show you that this kind of thing is both easy (well....relatively), but does take practice, and better yet...good design. think about how you want the form to work.

Oh, and templates MUST be called with File > New. Do not use the .dot file itself to make new files. Only open the .dot file to work on the template (master) itself.

Hope this helps.

Gerry
 
Hi Gerry,

Thank you for your help. I must say this is very helpful for me as it shows the step by step procedure. Well Done.

However, If i may ask something. After having put all the code in and save the file(test1.dot), you were saying

Use the menu File > New and select test1.dot as the template. It will make a new document (a clone) and the UserForm will display immediately and will not close until you fill in both fields. The OK button will put the information into the formfields.

I got this bit working. The userform pop up and when i filled out the two textfields, they were automatically filled into the fields. I notice that this is a new document right. I need to save it, don't i? I saved it as in test1.doc, not .dot. When i double clicked on the test1.doc, the userform did not pop up. Is that the way it works or? I guess what i'd like it to do is everytime someone clicks on the doc., the userform prompts user for specific details which are then put into the fields automatically. We're on the right track. It's just that i'm not sure if i saved it right or not that cause the userform not appearing when double clicking it.

Thank you very much Gerry and look forward to hearing from you
 
Hi Gerry,

I've got a quick question to ask. I seem to be unable to get that userform prompted again when i doubleclick on the test1.dot for the second time (after closing it once).

Your help is greatly appreciated. Thank you in advance.
 
This is because the userform1.show instruction (which displays the userform) is in Document_New of the template (.dot). Therefore it runs when the template makes a NEW clone.

To have it run when a document is opened then put it in Document_Open. Note that if you put it in Document_Open (of the template) it will NOT run make you make the clone. As you are not opening it.

If you want it to run when the clone is created (Document_New) AND when that clone is later opened, also put it in Document_Open.

These are design considerations, and should be clearly thought out. WHAT are you trying to do, and WHEN are you trying to do it.

Gerry
 
Gerry,

Sorry, it still doesn't work yet. Can i check with you?

I went back to open the test1.dot and to the Microsoft VisualBasic Project[test1]-[ThisDocument (code)], i added as follows

Sub Document_New()
UserForm1.Show
End Sub
Sub Document_Open()
UserForm1.Show
End Sub

Is that right? because i want it to run the userform whenever the test1.dot template is open. I left everything else the same.

Thank you in advance
 
I have one question. Are you using the template file (.dot) in this process? Because if you are...then you are using templates incorrectly. Template should be constructed, then NEVER opened - at least by the user. Templates should only be opened by whoever is building them.

But, yeah, put it in both places. Sigh.

WHY are you double clicking test1.dot? WHY? WHY? WHY? WHY are you opening it?

Gerry
 
Gerry,

Thank you for your reply.

Firstly, sorry for giving you a hard time.

I finally figured out why the userform doesn't show up when the document is open. What happened was remember you asked me to save the document as test1.dot which i did. However, this is where i made a mistake. I saved the test1.dot on my C:\desktop rather than in the template folder directed by office word.

Please correct me if i'm wrong. The reason i saved test1.dot onto my other folder is because i'd like the document to be shared by other users on the network. But since it has to be done that way, then i guess i don't have any choices, do i?... :)

Anyway, Gerry, Thank you for your patience and i really appreciated your help. Well done.
 
Templates can, and should, be set up so multiple users access them.

How you do this is a design consideration.

1. If the template is to be a COMMON (that is, used very often), you may want to consider setting it up as a global template. Global templates are not actually opened by Word. In fact, you can actually open them, change code in them, and the changes act in real-time. Globals load code, not files. Globals are loaded by putting them in Word Startup. It is in fact better to put them in Office Startup. This can be independent of the UserTemplates and WorkGroup Templates folders.

2. If it is for occassional use, then you can put it in the WorkGroup template folder. That way it can be shared. You would of course have to make sure all applicable users in fact DO use the same WorkGroup folder.

Choices? Of course you have choices. Tell me what you want it to be like.
Please correct me if i'm wrong. The reason i saved test1.dot onto my other folder is because i'd like the document to be shared by other users on the network. But since it has to be done that way, then i guess i don't have any choices, do i?... :)
I am not quite following. Has to be done WHAT way?

Again, if it is for multiple users, you can have it in a WorkGroup template folder. Technically speaking, it CAN be anywhere, just as long as your users are trained to go there using File > New. Do NOT open the .dot file. CALL it, using File > New.

Depending on your version, you could have the template in the Task Pane ready for them - regardless of where the actual file is located. There are LOTS of choices. What do you want to do?

If you feel this thread is somewhat completed, but wish to touch base on further suggestions, you can reach me at my handle, at telus(dot)net.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top