Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default 2 instances of excel

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 2 instances of excel

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default 2 instances of excel

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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 2 instances of excel

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

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default 2 instances of excel

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

"Tom Ogilvy" wrote:

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

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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










  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 2 instances of excel

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





"John" wrote in message
...
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...

"Tom Ogilvy" wrote:

Your either in for an eventual surprise or you didn't have two instances

of
Excel open. Good luck.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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










  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 2 instances of excel

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.

--
Regards,
Tom Ogilvy



"Tom Ogilvy" wrote in message
...
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





"John" wrote in message
...
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...

"Tom Ogilvy" wrote:

Your either in for an eventual surprise or you didn't have two

instances
of
Excel open. Good luck.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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












  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default 2 instances of excel

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!



"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy



"Tom Ogilvy" wrote in message
...
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





"John" wrote in message
...
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...

"Tom Ogilvy" wrote:

Your either in for an eventual surprise or you didn't have two

instances
of
Excel open. Good luck.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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













  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default 2 instances of excel

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...b;en-us;555191

and

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

--
Regards,

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

In article ,
says...
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



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default 2 instances of excel

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

"John" wrote in message
...
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!



"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy



"Tom Ogilvy" wrote in message
...
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





"John" wrote in message
...
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...

"Tom Ogilvy" wrote:

Your either in for an eventual surprise or you didn't have two

instances
of
Excel open. Good luck.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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

















  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default 2 instances of excel

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!


"NickHK" wrote:

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

"John" wrote in message
...
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!



"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy



"Tom Ogilvy" wrote in message
...
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





"John" wrote in message
...
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...

"Tom Ogilvy" wrote:

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

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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


"Tom Ogilvy" wrote:

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.

--
Regards,
Tom Ogilvy

"John" wrote in message
...
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
















Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't Allow Two Instances of Excel Stratuser Excel Discussion (Misc queries) 2 February 22nd 10 09:23 PM
Excel in Two instances [email protected] Excel Discussion (Misc queries) 3 January 28th 09 02:30 PM
Create list of unique instances from list of multiple Instances Dishon Excel Worksheet Functions 0 March 3rd 08 10:46 AM
How to get two instances of Excel Fred Holmes Excel Discussion (Misc queries) 3 May 23rd 05 04:52 PM
Is there an easy way to replace list A instances with list B instances (sorted lists). kkip Excel Programming 3 December 31st 03 01:50 PM


All times are GMT +1. The time now is 06:48 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"