View Full Version : Solved: Closing Excel from Word

12-13-2004, 06:39 AM
In my ongoing TextSelect utility, (posted here for a modified use http://www.vbaexpress.com/forum/showthread.php?goto=newpost&t=1466)
the excel process is left running when the userform is closed. The Excel application is not visible while the Word Userform is open, unless it was visible perviously. The code I'm attempting to use is as follows:

Private Sub UserForm_Terminate()
Dim Datafile As String, DataRoute As String, MyXL As Object
DataRoute = MyPath.Text & MyFile.Text
Datafile = MyFile.Text
If Tasks.Exists("Microsoft Excel - " & Datafile) = True Then
Set MyXL = GetObject(DataRoute)
End If
On Error Resume Next
MyXL.Windows(Datafile).Close False
Set MyXL = Nothing
End Sub

I've also tried variations on this, but without success.

12-13-2004, 07:43 AM
How about MyXL.Quit right before the Set MyXL = Nothing?

Jacob Hilderbrand
12-13-2004, 07:47 AM
Yeah, basically is you start an instance of Excel, set some variable to indicate that you want to Quit the Excel App at the end. Then just use an If statement to check if you should Quit Excel or not.

Then like Steiner says just use MyXL.Quit to quit that instance of Excel.

12-13-2004, 11:09 AM
Thanks both,
I'd tried the quit before, but I think my IF statement was preventing the MyXl value from being set. :blush When I get rid of it, the Quit function works correctly.