Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Don't Allow Two Instances of Excel | Excel Discussion (Misc queries) | |||
Excel in Two instances | Excel Discussion (Misc queries) | |||
Create list of unique instances from list of multiple Instances | Excel Worksheet Functions | |||
How to get two instances of Excel | Excel Discussion (Misc queries) | |||
Is there an easy way to replace list A instances with list B instances (sorted lists). | Excel Programming |