2 instances of excel

J

John

Could someone please!!! help me close a second instance of excel. If it
helps, the file name is ("c:\blp\data\t0983103.xls")...

Or help me understand if this is on the right track...

' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName as String, _
ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long, _
ByVal wParam as Long, _
ByVal lParam As Long) As Long

Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
Set MyXL = Getobject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel

' Set the object variable to reference the file you want to see.
Set MyXL = Getobject("c:\vb4\MYTEST.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
Do manipulations of your file here.
' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End IF

Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub
 
T

Tom Ogilvy

I have never seen any code that translates a handle to the application
window into an object instance of the application so you can manipulate it
using automation (although I am sure there must be a way to do it).

Possibly you could use the handle to bring it to the top and use send keys
to close it.

You could possibly identify it as a process and kill the process

However, the more important question may be how it got opened. Perhaps you
can avoid the problem.
 
J

John

I found this to work (I made a minor change or two)... that seems to be the
way it goes, you finally break down and post and then figure it out...

Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = Window.Close("Microsoft Excel - t0983103.xls")
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
'SendMessage hWnd, WM_USER + 18, 0, 0
Application.DisplayAlerts = False
Application.Quit
Application.DisplayAlerts = True
End If
End Sub
Sub CloseReport()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel

' Set the object variable to reference the file you want to see.
Set MyXL = GetObject("c:\blp\data\t0983103.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
MyXL.Application.Quit
MyXL.Parent.Windows(1).Visible = True
'Do manipulations of your file here.
' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End If

Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.
End Sub
 
T

Tom Ogilvy

Your either in for an eventual surprise or you didn't have two instances of
Excel open. Good luck.
 
J

John

If I don't have two instances of excel open... it will close even the
"master" file... since I have an application.quit in there. However, as I
said, it seems to run fine with two seperate excel instances... Why does
this seem surprising?

ps - i did not write the code, only modified small portions of it...
 
T

Tom Ogilvy

It is easy to recognize the code example from the getobject help example.
GetObject doesn't discriminate between running instances of excel and you
can't specify which instance nor is there a format that supports that. Nor
is there a way to step sequentially through instances. If it closed the
correct instance or returned a reference to the correct instance, then you
were lucky.
--
Regards,
Tom Ogilvy
 
T

Tom Ogilvy

Although I did some testing and using the file name appeared to work each
time - so maybe using the filename is the way to do it - but that is not
what the help says.
 
J

John

Yes, I have understand this is not supposed to be able to work... and I
honestly don't know enough about VBA or this code to be able to say why it
works. I can guess, but... I dunno. Overall, it is interesting, and maybe
others who are more expirienced than I will find this helpful/interesting for
future reference.

By the way, thanks for helping make this a great site for info!
 
T

Tushar Mehta

I am not sure what you are trying to do or what you are doing, but it
seems to be overly complicated.

First of all, start by instantiating the application object and not the
workbook. Then, ensure all references to the instantiated application
go through your variable. This will ensure that no behind-the-scenes
copy of the application exists when you believe you are done with it.

See
How to safely instantiate another Office application and close it only
if you started it
http://support.microsoft.com/default.aspx?scid=kb;en-us;555191

and

Program won't quit
http://www.tushar-mehta.com/excel/vba/xl_doesnt_quit/index.htm

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
N

NickHK

John,
Maybe I am missing something, but I just don't get the point of this code.
If GetObject(,"Excel.Application") fails, how is FindWindow("XLMAIN", 0)
going to succeed ?

And Set MyXL = GetObject("c:\blp\data\t0983103.XLS") will always succeed,
assuming the filename is valid.

And this is Excel VBA:
Application.DisplayAlerts = False
Application.Quit
Application.DisplayAlerts = True
So how can GetObject(,"Excel.Application") possibly fail ?

Also, what is "Window.Close(......" ?

Confused.

NickHK
 
J

John

Nick,

All I was trying to do was close an open file in a second instance of
excel... I had no idea how to do that... and looked for some code to be able
to do that. I came across the code I posted and then began to play with it
myself. I still do not understand it, as I said earlier, but somehow it
works for what I was trying to do. I could be and I am sure it is, very
inefficient, but I have only been working with VBA for 2.5 months and so I
really couldn't say what is going on with it fully...

Sorry for the confusion, but I think it is cool because something in there
works!
 

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