Saving a doc that was created by a template

J

JeffH13

I use a MS Word template to create an order form. The form was originally
created by Planit Solid a cad kitchen drafting software company. I can open
the template and get to the code in the MS VB area and if have done some work
to it already.

Once the the order form has been created and we save it, if we reopen it, it
tries to run the macro again after it has already filled the form. True I
can just cancel out but my sales person get annoyed by it.

Is there a way to save it after the form has been filled in, or not to save
it with the VB, or have it when it creates the second for not to have the VB
in the second form...

Open for idea's... thanks....
 
G

Graham Mayor

The principle of using templates is that you create new documents from them.
The documents themselves contain no macros.
If your macro runs on opening the document or template, then it is called
AutoOpen. If the template is to be used correctly it would be called AutoNew
and would only run when a new document was created.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

JeffH13

I looked in the macro area there are no macro's listed
----
Private Sub Document_Open()

Dim loDoc As Document
Dim lsDotName As String

'Creates new document based on template
lsDotName = ActiveDocument.Name
Documents.Add Template:=ActiveDocument.FullName, NewTemplate:=False

'Removes dot file
For Each loDoc In Documents
If LCase(loDoc.Name) = LCase(lsDotName) Then
loDoc.Close savechanges:=False
End If
Next

End Sub
 
G

Graham Mayor

That function works as AutoOpen and appears to do nothing more useful than
create a document when the template is opened. I would have thought that it
would make more sense to lose that function, save the template as a template
in the user templates folder and create new documents from it. The user has
no need to open the template.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

JeffH13

Ok... I more confussed than before. First Question where can I find if it is
using this AutoOpen or not.

How this is setup is Word opens this *.dot file and a form comes up about
shipping information, you fill it in it ok and it files in the rest of the
form with information from a database. Then you hit file, save and it save
it as a *.doc file but when you reopen it it does the same routen and will
try to write over the top with what ever information is in the database.
 
G

Gordon Bentley-Mix

Jeff,

First a little background on the "mechanics" of Word macros. From the VBA
help topic on Auto Macros:

By giving a macro a special name, you can run it automatically when you
perform an operation such as starting Microsoft Word or opening a document.
Word recognizes the following names as automatic macros, or "auto" macros.

This help topic goes on to describe in some detail the various types of
"auto" macros, including the name of each of these special macros and when it
runs. The important one for you in this instance is:

Name: AutoOpen When it runs: Each time you open an existing document

Auto macros are created by either writing a procedure (sub) in any normal
code module using one of the keyword "auto" macro names, or by creating a
code module using one of these names and writing a procedure called "Main" in
this module. I would recommend reviewing the VBA help topics on this subject
for more information; search for "Auto Macros".

HOWEVER...

There are other ways to invoke a macro automatically. This is called
"event-based" code, which means it runs in response to something happening
(an event) in the Word environment. All sorts of things in Word have events
associated with them. For example, if you look at the code behind the
UserForm in your template, you'll probably see a procedure called something
like "Private Sub cmdOK_Click". This is code that's invoked in the "Click"
event of the "OK" CommandButton on the UserForm. (The name of the actual
procedure may be slightly different depending on what the name of the
CommandButton is, but you get the idea.)

What's important in your particular situation is understanding that
documents have events associated with them as well. These include a "Close"
event, a "New" event and (key for you) an "Open" event. MSFT was kind enough
to make the names of these events pretty self-descriptive, so I'm sure you
can work out what each one is.

Keeping in mind the basic code structure described in the discussion of
"auto" macros above and the way that "event-based" code is invoked, it
becomes apparent how the code you posted works: it runs in response to the
"Open" event of the document (Private Sub Document_Open). So in your case,
rather than having a AutoOpen macro you have a Document_Open macro that's
running whenever you open that particular document.

Now, having said all that, let's look at how to get around the problem
you're asking about.

The first place to look is at the process you're using to create a new
document from your template. You said that it's set up so that "Word opens
this *.dot file". I'm assuming that this means that you click "File |
Open..." and navigate to the template and actually _open_ it. This makes
sense since the code you posted is intended to run when the document (or in
this case, the template) is opened. And as Graham pointed out, this code is
actually pretty useless; all it does is create a new document from the
template - something that Word does natively, so there's really no need to
write code to do it.

However, this really isn't the best way to use a template because, as I said
above, Word will automatically create a new document from a template when you
click "File | New..." and select the desired template. And when this new
document is created, it doesn't contain any code - especially no "AutoOpen"
or "Document_Open" code. The code remains in the template, although if the
user has access to the template this code is still available for use in the
document - but that's a subject for another discussion.

What's missing from the information you have provided so far, however, is
the code that actually displays the UserForm you mentioned. I suspect that
there _is_ an "auto" macro lurking about somewhere in the template - most
likely an "AutoNew" macro (or possibly a "Document_New" macro) with code in
it that simply says something like:

Sub AutoNew()
UserForm1.Show
End Sub

(BTW, this is actually *really* sloppy code, but it works so I wouldn't
really worry about it.)

In the end, though, it doesn't really matter too much how the UserForm gets
displayed. The problem you're asking about is related to stopping the
UserForm from being displayed every time you re-open one of your documents.
I'd recommend doing a couple of things to see you can fix the problem.

NOTE: Before you do ANYTHING make a back up copy of your template as it
exists now.

First, try creating a new document from the template using the "File |
New..." process I discussed above. If the UserForm is displayed, then this
confirms that there is some sort of AutoNew / Document_New macro running
someplace. You could just stop there and change your process to use "File |
New..." instead of "File | Open...", but I suspect that won't entirely
resolve the problem - especially since the code you posted appears to be
doing something similar but you're still having problems.

Second, _open_ the template while holding down the Shift key (which should
stop the Document_Open macro from running) and then go into the VBA editor
and comment out the code in the Document_Open macro. You can do this by
either putting a single quote in front of every line of code or by selecting
the entire procedure (from Private Sub to End Sub) and clicking on the
"Comment Block" button. Then save the template and create a new document from
it using "File | New..." Finally - and this is the most important part of
this test - close the new document and re-open it and see if the UserForm is
still coming up. If things appear to still work OK, this confirms what Graham
said about the code being useless. It also resolves your issue; just leave
the code commented out and you should be good to go.

If you run into problems, post back. If all else fails, contact me at the
email address in my profile and I'll try to sort it out.
-----
Cheers!

Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

Jeff,

I just re-read your original post, and something caught my eye that I missed
before. You said that this template was created by a software vendor. I also
note in your latest post a reference to filling in the form with information
from a database. This raises an important question: Is this template being
used as part of an automated process invoked from your Line-of-Business
application (the "cad kitchen drafting software" mentioned in your OP)?

If so then this may go some way toward explaining why the Document_Open code
was written in the first place. I suspect that perhaps the software vendor
couldn't work out a way to invoke a Document_New event in Word, so instead
they are using a system API call (or something similar) to open the specified
file and writing VBA to simulate the Document_New event. Dirty, messy coding.
Bad developer! No biscuit!

Unfortunately, it also complicates things a bit. Simply commenting out the
Document_Open code won't fully resolve the problem; instead it will just move
the problem from the individual users to the LoB software...

~sigh~

I'm sure there's a solution for this problem somewhere, but generally in
situations like this I recommend to my clients that they contact the software
vendor and ask them to sort it out. It's not the best idea to start mucking
around with someone else's code - especially without permission. It tends to
cause all sorts of unexpected consequences, and you can also be liable for
copyright infringement.

If the vendor won't help then I can only suggest finding a good (and local)
VBA developer to come onsite and give you a hand. (Make sure that you inform
the vendor of your intended actions first so as to mitigate any risk from the
aforementioned copyright infringement.) This is something that's probably
best not handled remotely or by someone without a good deal of VBA skill. If
the developer is onsite then he/she will be able to see _exactly_ what's
happening - especially what the LoB software is doing - and develop a
solution that fits the conditions.
-----
Cheers!

Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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