Closing inactive document

R

Robert_L_Ross

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!
 
D

David

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
 
R

Robert_L_Ross

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!!!
 
R

Russ

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
 
R

Russ

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.
 
R

Robert_L_Ross

Russ,

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

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