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 / August 2007

Tip: Looking for answers? Try searching our database.

Closing inactive document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert_L_Ross - 31 Jul 2007 20:56 GMT
I have a template that I set up that links to a spreadsheet.  The userform in
the .dot gets a list of school names from the Excel document, then presents
those in a userform for the user to select.  When they select the school, it
populates the bookmarks I set up and the user completes the form.  When they
are done, I have a macro/toolbar setup to save the document using some of the
bookmarks inthe document.

Now comes the fun part...if the user wants to complete another form, I want
to open a new document, but then immediately close the saved document.

.dot opens a doc (document1)
User completes document1
User saves document1 as uniquenameddoc1
Macro needs to have .dot open a new doc (document2), then close
uniquenameddoc1

Here's my code:
Sub SaveFormAs()

   Dim pFileName As String

   pFileName = Year(Now()) & Format(Month(Now()), "00") & _
                      Format(Day(Now()), "00") & "-" & _
                      ActiveDocument.FormFields("SchoolName").Result & "-"
& _
                      ActiveDocument.FormFields("APP_NAME").Result
   
   ActiveDocument.SaveAs "G:\Forms\" & pFileName & ".doc"

End Sub

Sub newform()

   Dim CURRENTDOCNAME As String
   
   CURRENTDOCNAME = ActiveDocument.FullName
     
   Word.Documents.Add Template:="G:\forms\Correction Sheet.dot"
   
   Windows(CURRENTDOCNAME).Close

End Sub

This opens the new form, but it doesn't close the old form(s) until I close
my last document.

Am I missing something here?

Thanks!
David - 01 Aug 2007 09:24 GMT
Try this: it will close the currently active document and then open the new
document, if the document has not been saved the user will be prompted to
save it.

Sub newform()

   ActiveDocument.Close
   Word.Documents.Add Template:="G:\forms\Correction Sheet.dot"

End Sub

It looked like it would be ok to close the current document before starting
the new one but if not try this:

Sub newform()

   Dim CURRENTDOCNAME As String
   CURRENTDOCNAME = ActiveDocument.FullName
   Word.Documents.Add Template:="G:\forms\Correction Sheet.dot"
   Documents(CURRENTDOCNAME).Close

End Sub

David

> I have a template that I set up that links to a spreadsheet.  The userform in
> the .dot gets a list of school names from the Excel document, then presents
[quoted text clipped - 45 lines]
>
> Thanks!
Robert_L_Ross - 01 Aug 2007 18:28 GMT
David,

The first code example closed the form and didn't open a new one (maybe
since it was based on a template?).

When I use the 2nd code example, it closes the documents when I close the
UserForm on the new document (since the Userform is an On New type of thing.

This works though, since I would only have two documents open maximum, and
one would close when I select and close my user form.

Thanks for the help!!!

> Try this: it will close the currently active document and then open the new
> document, if the document has not been saved the user will be prompted to
[quoted text clipped - 70 lines]
> >
> > Thanks!
Russ - 04 Aug 2007 08:29 GMT
Are you invoking the userform from the form that is open via the template?
That situation would explain why the form can't be closed until the userform
it invoked is closed.

When juggling multiple documents (possibly from multiple applications) it is
best to assign each document to a variable for reference, instead of relying
on ActiveDocument.

Dim objDocument As Document
Set objDocument = Documents.Add ...
...
objDocument.Close

> David,
>
[quoted text clipped - 85 lines]
>>>
>>> Thanks!

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 04 Aug 2007 20:04 GMT
If your users are not using Word97 or MacWord, you have the option to open a
userform as modal(vbModal)(the default) or modeless(vbModeless).

(snip)
Modal vs. Modeless:
The definition of a modal form is that it must be closed (hidden or
unloaded) before you can continue working with the rest of the application
(hence it is also shown on top; but this is rather a "side effect" so to
speak). For example, a dialog box is modal if it requires you to click OK or
Cancel before you can switch to another form or dialog box.

Modeless dialog boxes let you shift the focus between the dialog box and
another form without having to close the dialog box. You can continue to
work elsewhere in the current application while the dialog box is displayed.
Modeless dialog boxes are rare. From the Edit menu, the Find dialog box in
Visual Basic is an example of a modeless dialog box.
(snipped taken from the MSDN library)

Caution:a modeless userform allows the user to change non-userform windows
while the form is showing.

(snip)
In VBA, you can show a form as vbModal or vbModeless. In Visual Basic .NET,
the ShowDialog method is used to display a form modally; the Show method is
used to display a form non-modally. Note, however, that the form will
display non-modally, but when you click in the Word or Excel document, the
form moves to the background, which can be confusing to your users.
(/snip)

See also:
<http://support.microsoft.com/kb/171978/EN-US/>

So if you invoking the userform from the *document* that is open via the
template, use, i.e.:
MyUserForm.Show vbModeLess
Then that connection to the invoking document may be separated enough to
allow closing of that document while the userform is still in memory.

Since I don't have access to anything but Word97 or MacWord, I can't test if
you can change a form from modal to modeless, with a simple .hide in
between, or if once a form is in memory, only Windows API calls can change
the form window attributes.

But since a userform can't unload itself and reload itself from/to memory,
template code or a third party code, would have to do that, to change a
modal userform to a modeless userform just before you want to close the
invoking document and back to modal after closing the document.

> Are you invoking the userform from the form that is open via the template?
> That situation would explain why the form can't be closed until the userform
[quoted text clipped - 101 lines]
>>>>
>>>> Thanks!

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Robert_L_Ross - 06 Aug 2007 01:24 GMT
Russ,

Appreciate the post, but the quesiton was asked and answered (days) before
you posted.

> David,
>
[quoted text clipped - 83 lines]
> > >
> > > Thanks!
Jonathan West - 06 Aug 2007 08:53 GMT
> Russ,
>
> Appreciate the post, but the quesiton was asked and answered (days) before
> you posted.

Robert

To help those trying to help others, so we don't waste time answering a
question that has already been answered, please don't post separate copies
of the same question to multiple groups.

If you must post a question to more than one group, include all the groups
in the header of a *single* message. That way, any reply will also go to all
the groups, and wherever the message is seen, we will be able to see if
there is an adequate reply already available.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

 
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.