Find a document and clsoe it

  • Thread starter RayJ_BirminghamUK
  • Start date
R

RayJ_BirminghamUK

Hello everybody

I am a novice VBA programmer and started what seemed like a simple bit of
programming but it has proven to be harder than I thought.
Basically I have a form template (TemplateA) which is stored centrally and
accessed by many users. I need to replace this with a newer version
(TemplateB) don’t wish to remove the old version in case there are documents
still in use with references back to TemplateA
I created an AutoNew Macro in TemplateA which pops up a message box to say
you are using an old version and will be redirected to the new template
(TemplateB)
That works fine except I want to close the document created by the user
double clicking on TemplateA .
How do I find the document and close it leaving TemplateB open and active.
Templates were created in Word 2000 and are used in either Word 2000 or Word
2002.
Thanks in advance for any help you can offer.
 
J

Jay Freedman

This ought to work:

Sub AutoNew()
Dim nDoc As Long

For nDoc = Documents.Count To 1 Step -1
If (Documents(nDoc).Type = wdTypeDocument) And _
(Documents(nDoc).AttachedTemplate.Name _
= ThisDocument) Then
MsgBox "This document is based on an " & _
"obsolete template and will close now."
Documents(nDoc).Close _
SaveChanges:=wdDoNotSaveChanges
End If
Next
End Sub

The For loop checks every open document, and closes any that match the
criteria in the If statement.

The check of the document type allows you to open the template itself for
editing without the macro slamming it closed immediately. (I hate when that
happens. <g>)

The other part of the If statement asks whether the attached template (the
one on which the document is based) has the same name as the template that
contains the running macro. It doesn't matter what that name is, as long as
it matches.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
F

fumei via OfficeKB.com

"The check of the document type allows you to open the template itself for
editing without the macro slamming it closed immediately. (I hate when that
happens. <g>)"

Thanks for the laugh Jay. Yup. Yup. That is a pain for sure. Been there.
 

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