PDA

View Full Version : Controls with word template



bigdan43
08-22-2008, 10:50 AM
I have a form that I've created in word and instead of placing formfields I placed actual controls like comboboxes and text boxes as well as labels.

I liked this because I can easily code the buttons to interacts with each other using click events.

The problem is that although it works fine when I "open" the template, when it is opened as "new" none of the code from "thisdocument" gets transferred to the new document making the form useless.

Should I be doing this another way or is there something I can do to keep the code?

CreganTur
08-22-2008, 11:41 AM
Welcome to the forum, always good to see new members!


The problem is that although it works fine when I "open" the template, when it is opened as "new"

This is normal... and it's the way it's supposed to work. When you open a Template, what is actually opened is a Clone of the template. All of the work you do will be on the Clone- this keeps the template from being changed... which is the whole point of a template.


none of the code from "thisdocument" gets transferred to the new document making the form useless
It's there- you just can't use the Document_Open event to launch your Form. You need to use Document_New- this is the event that's triggered when you open a Template, because (as stated above) it will create a 'new' clone of the template- it doesn't actually open the template.

HTH:thumb

bigdan43
08-22-2008, 12:46 PM
This is the code that I have to start with and it just sets of a couple comboboxes, the code definately does not exist in the new document. Normally I'd initialize this on document_New() but the way this form works, I use code in the userform to trigger autotext to fill the document with either a french or english form.

I would use formfields if I could figure out how to initialize them from my userform.


Private Sub cb_Hospitality_DropButtonClick()
If cb_Hospitality.ListCount = 0 Then
cb_Hospitality.AddItem "Breakfast"
cb_Hospitality.AddItem "Lunch"
cb_Hospitality.AddItem "Dinner"
cb_Hospitality.AddItem "Reception"
End If
End Sub
Private Sub cb_Hospitality1_DropButtonClick()
If cb_Hospitality1.ListCount = 0 Then
cb_Hospitality1.AddItem "Breakfast"
cb_Hospitality1.AddItem "Lunch"
cb_Hospitality1.AddItem "Dinner"
cb_Hospitality1.AddItem "Reception"
End If
End Sub
Private Sub cb_Hospitality2_DropButtonClick()
If cb_Hospitality2.ListCount = 0 Then
cb_Hospitality2.AddItem "Breakfast"
cb_Hospitality2.AddItem "Lunch"
cb_Hospitality2.AddItem "Dinner"
cb_Hospitality2.AddItem "Reception"
End If
End Sub
Private Sub cb_Hospitality3_DropButtonClick()
If cb_Hospitality3.ListCount = 0 Then
cb_Hospitality3.AddItem "Breakfast"
cb_Hospitality3.AddItem "Lunch"
cb_Hospitality3.AddItem "Dinner"
cb_Hospitality3.AddItem "Reception"
End If
End Sub
Private Sub cb_Hospitality4_DropButtonClick()
If cb_Hospitality4.ListCount = 0 Then
cb_Hospitality4.AddItem "Breakfast"
cb_Hospitality4.AddItem "Lunch"
cb_Hospitality4.AddItem "Dinner"
cb_Hospitality4.AddItem "Reception"
End If
End Sub


Private Sub Document_New()
UserForm1.Show
End Sub

lucas
08-23-2008, 05:13 AM
It's there- you just can't use the Document_Open event to launch your Form. You need to use Document_New- this is the event that's triggered when you open a Template, because (as stated above) it will create a 'new' clone of the template- it doesn't actually open the template.

HTH:thumb

Hi Randy, I've read this a couple of times trying to get a handle on what the real problem is.

I don't think the code will be in the newly cloned document. It will be referenced to the template and can be run as long as the template is available to the clone but if the clone is sent to someone elses machine which does not have the template then the code is not available and so it can't be run.

When you create a clone with your template go to the VBE and check out the thisdocument modules that show.

In the project explorer you will find the new document and you will also find the template. If you check out the thisdocument modules for each document you will find that there is no code in the clone but it will show up in the original templates thisdocument module.

The only simple and effective way I have found to make the code accompany the document is to not use a template. You just need to make a copy of the file you wish to use each time and then the code will stay with the document. In this case you would not use document new at all, just document open if you want the code to run each time the doc is opened.

I hope I understand the problem correctly.

bigdan43
08-23-2008, 07:34 AM
I was coming towards that same answer :bug:
Is there any way of copy+pasting code to the doucment itself using a macro inside a module? I imagine there isn't :dunno

fumei
09-01-2008, 05:06 AM
You can write code modules to a file using VBA.

bigdan43
09-01-2008, 02:54 PM
The modules stay with the new document, it's the trigger's code that won't follow with :)