Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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
|
|||
|
|||
![]()
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 | |
|
|
![]() |
||||
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 |