Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open & Close Apps

Status
Not open for further replies.

ablackwe

Programmer
May 14, 2004
57
GB
Can someone please help with a problem I have. I have a number of command buttons on a Word document that each opens a seperate document or spreadsheet (similar to hyperlinks). The problem I have is when one button is clicked, it needs to check if any of the other docs are open, if so, close them down before continuing. Any ideas on how this can be done?

Thanks
Adam
 
If you don't mind all open documents being closed, before the button opens another document, you could use:
Code:
    Documents.Close
in the button's VBA code. This command closes all open documents - Word will still prompt you to save any unsaved changes, before closing the document/s.

This is the equivalent of holding down the [Shift] key, then choosing the File ... Close All menu option.

Bob Stubbs
 
Hi blackwa,

You know (or can arrange to know, with a global variable perhaps) what the previous document opened was, so you should be able to close it. Easy if it's a Word document. If its a spreadsheet it might be a bit more complex. It depends on how you opened it and what may have happened since. Do you use an existing instance of Excel, for example, if there is one? Has the user opened another workbook between pushing buttons, etc?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
The problem, or requirements, are not totally clear. However, if what you want to know is:

IF Document blahblah.doc is open, then close it,

then this is easy. You can use the Documents collection. The following makes up an array of known documents. You may not need this - that will depend on what requirements you have. I put it in just in case you may need to keep SOME documents open, just specific ones to close. I also made it have a message stating the doc will close - you could adjust this to simply close it.

If you state most precisely what you need to happen, this could probably be tuned to be smaller and more precise.

Code:
 Sub CommandButton1_Click()
Dim doc As Document
Dim var
Dim i As Integer

Dim myDocs(4) As String
myDocs(0) = "This is Document 1.doc"
myDocs(1) = "This is Document 2.doc"
myDocs(2) = "This is Document 3.doc"
myDocs(3) = "This is Document 4.doc"
myDocs(4) = "This is Document 5.doc"

For Each doc In Application.Documents
 For var = 0 To UBound(myDocs)
    If doc.Name = myDocs(i) Then
        MsgBox doc.Name & " is open, and will be closed."
        doc.Close wdDoNotSaveChanges
        Exit For
    End If
    i = i + 1
 Next
 i = 0
Next
End Sub

Gerry
See my Paintings and Sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top