Prevent opening Word Template more than once

D

DeanJ

I have a very specific requirment. I have to ensure that a specific
Word template is never opened more than once. I'm new to VBA so this
maybe the easiest thing in the world but so far it has driven me to
multiple injured keyboards and dead mousies!


Any help would be MUCH appreciated.


Thanks,


Dean
 
G

Greg

Dean,

Can you be more specific? What do you mean by "never opened more than
once?" Are you trying to open it in a routine, do something, close it
and them prevent it from reopening, or have you just created a template
and you don't want anyone to be able to open it?
 
D

DeanJ

Specifically here is what I need:

I am giving a template to users which includes several VBA forms and
allows them to make a series of selections and bring in data from
multiple sources and then launch one of several letter templates using
the gathered data. The user will launch the template merely through a
shortcut or double-clicking the template file itself.

However, if the template is already open, I do not want them to be able
to double-click it again and open a 2nd copy. I assumed I could
prevent this by checking the documents collection to see if any open
document had this template as the attached template, but when I
double-click the template it opens a NEW instance of Word and then it
is only aware of documents opened in its own instance and ignores the
fact that the template is opened in another instance.

So, I need to be able to either check ALL instances of Word to see if
this template is in use and then prevent it opening again, OR apply
some property or code that restricts the template to only being opened
once.

I'm very new to VBA and anything other than general use of Word so I
have no idea what options are at my disposal.

Thanks for any help you can provide.
 
G

Greg

Dean,

I am not sure if this is the "best" or even a sound way of doing this,
but offer it as a suggestion.

First you don't open a template by double clicking it. Double clicking
a template opens a new document based on that template. Am I corrrect
in assuming that you don't want your users to have two documents baseed
on the same attached template open simultaneously? This might work.
Put both macros in a module stored in the your template.dot file
(replace Test.dot with Your Template Name.dot):

Sub AutoOpen()
Dim oDoc As Document
Dim i As Integer
For Each oDoc In Application.Documents
If oDoc.AttachedTemplate = "Test.dot" Then
i = i + 1
End If
Next
Set oDoc = ActiveDocument
If i = 2 Then oDoc.Close
End Sub
Sub AutoNew()
Dim oDoc As Document
Dim i As Integer
For Each oDoc In Application.Documents
If oDoc.AttachedTemplate = "Test.dot" Then
i = i + 1
End If
Next
Set oDoc = ActiveDocument
If i = 2 Then oDoc.Close
End Sub
 

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