Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Find a document and clsoe it

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RayJ_BirminghamUK - 08 Oct 2007 17:43 GMT
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.
Jay Freedman - 08 Oct 2007 18:44 GMT
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.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hello everybody
>
[quoted text clipped - 14 lines]
> Word 2000 or Word 2002.
> Thanks in advance for any help you can offer.
fumei - 09 Oct 2007 04:34 GMT
"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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.