Template

J

Julie

Hi - I have a template called Letterhead. It contains a
user form and some simple code. When I choose File, New
and select "Letterhead" it appears with the user form and
works as expected. When I choose File, New again, and
select the same template, it creates a new document in the
background, but adds the text I enter into the user form,
in the current, on-screen letter. So it does create the
new document, but adds the text to the bookmarks in the
current document. Strange! My other templates do not
have this problem. Help! Here's the code:

Private Sub Document_New()

With frmLetterhead

.txtName = ""
.txtDD = ""
.txtIntAdd = ""
.txtName.SetFocus
End With

frmLetterhead.Show

If btnOKClicked = True Then

ActiveDocument.Bookmarks("bkName").Range.Text =
frmLetterhead.txtName
ActiveDocument.Bookmarks("bkDD").Range.Text =
frmLetterhead.txtDD
ActiveDocument.Bookmarks("bkIntAdd").Range.Text =
frmLetterhead.txtIntAdd

Else
ActiveDocument.Close wdDoNotSaveChanges

End If

Selection.EndKey Unit:=wdStory

End Sub

Thanks for your help.
 
M

Malcolm Smith

Julie

I have seen this happen before. But not often. The problem is that you
are using what is known as 'magic user forms' which really should be
discouraged.

I have had an, ahem, discussion with others here in the past when I said
that this sometimes occurs when magic forms are used.

What are magic forms? When you do what you have done and used
frmLetterhead as an object and not as a class. Going back a step to
explain the difference between the two; a class is a blueprint of an
object, the architect's plans if you like. The object is what is made
from the blueprint.

In VB and VBA it is possible to use the class as an object. In this case
a default instance of the class is formed and, most of the time, it works
well. Sometimes, it doesn't.

As in this case. And this is where I had the previous 'discussions' with
an MVP about this as he challenged me to locate a reproducable incidence
of this situation. Of course, I couldn't.

However, what I can do is to teach you the non-lazy of programming the
creation of an object.


In your code you have to declare a form object at the top of the routine:

Dim ofrmLetterhead as frmLetterhead

This line means that you are declaring a ofrmLetterhead object which is of
the class type frmLetterhead.

Then in the code, near the top you create the object:

Set ofrmLetterhead = New frmLetterhead

Then you work with the ofrmLetterhead object from now on. Also you must
remember to destroy the ofrmLetterhead at the end. Anyway, this ought to
be your full code now:



Private Sub Document_New()

Dim ofrmLetterhead as frmLetterhead

' Create the instance
Set ofrmLetterhead = New frmLetterhead
With ofrmLetterhead

.txtName = ""
.txtDD = ""
.txtIntAdd = ""
.txtName.SetFocus
End With

ofrmLetterhead.Show

If btnOKClicked = True Then

ActiveDocument.Bookmarks("bkName").Range.Text =
ofrmLetterhead.txtName
ActiveDocument.Bookmarks("bkDD").Range.Text =
ofrmLetterhead.txtDD
ActiveDocument.Bookmarks("bkIntAdd").Range.Text =
ofrmLetterhead.txtIntAdd

Else
ActiveDocument.Close wdDoNotSaveChanges

End If

' Destroy the instance of the ofrmLetterhead object
unload ofrmLetterhead
set ofrmLetterhead = Nothing

Selection.EndKey Unit:=wdStory

End Sub




Do let me know what happens now.

I have made some comments about this on my site somewhere. Do have a look
at the Tutorial on making a template.

I hope that this helps
Malc
www.dragondrop.com
 

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