![]() |
Finding Excel application handle
I have a number of instances of Excel running each containing a number of workbooks
One of the instances needs to determine the worksheet names in all the workbooks I can use Application.Workbooks().worksheets().name to get the names from the current instance but I am unable to get the information from all the other instances as I am unable to get an Applicatio handle for each instance Any ideas how I can get the handles to the other instances of Excel Cheers Mark |
Finding Excel application handle
Hi Mark,
If you already know the workbooks name you can just use the full pathname instead of the classname. Set MyWorkBook=GetObject("C:\my documents\myfile.xls") If it is a seperate instance, not multipul workbooks in one Excel instance from Windows task list. This code will find all running instances' and you can modify the code to look for a specific instance if you know its name. <http://www.google.com/groups?hl=en&lr=&ie=UTF-8&selm=%23d59pH8nCHA.2288%40TK2MSFTNGP10 -- John johnf 202 at hotmail dot com "Mark Grimes" wrote in message ... | I have a number of instances of Excel running each containing a number of workbooks. | | One of the instances needs to determine the worksheet names in all the workbooks. | | I can use Application.Workbooks().worksheets().name to get the names from the current instance, | but I am unable to get the information from all the other instances as I am unable to get an Application | handle for each instance. | | Any ideas how I can get the handles to the other instances of Excel. | | Cheers, | Mark. | |
Finding Excel application handle
Thanks for that Joh
However, although it gives me the window handle for all the Excel instances' what I then need to do is to obtain the worksheet names within the Excel instances' I do not know how I can examine the contents of the Excel workbooks in each instanc with only the window handle What I really need is the Application handle for each instance so that I can us Application.Workbooks(x).Worksheets(y).nam Any ideas ? Cheers Mark. |
Finding Excel application handle
Hi Mark,
We may be too far getting ahead of the problem. "Workbooks" is a collection. So is "Sheets". Once you get a handle to the object it is easy to find the workbooks and sheets within. In the code below MyXL is the object name. Copy & paste the code into a new workbook and run the sub "ListWBSheetNames". ' 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 Dim MyXL As Object ' Variable to hold reference to Microsoft Excel. Dim ExcelWasNotRunning As Boolean ' Flag for final release. Sub GetExcel() ' 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 Sub ListWBSheetNames() Dim wb, MySheets GetExcel For Each wb In MyXL.Workbooks Debug.Print wb.Name For Each MySheet In MyXL.Sheets Debug.Print MySheet.Name Next Next Debug.Print MyXL.Workbooks.Count, MyXL.Sheets.Count End Sub -- John johnf 202 at hotmail dot com "Mark Grimes" wrote in message ... | Thanks for that John | | However, although it gives me the window handle for all the Excel instances', | what I then need to do is to obtain the worksheet names within the Excel instances'. | | I do not know how I can examine the contents of the Excel workbooks in each instance | with only the window handle. | | What I really need is the Application handle for each instance so that I can use | Application.Workbooks(x).Worksheets(y).name | | Any ideas ?? | | Cheers, | Mark. |
Finding Excel application handle
Hi Mark,
GetObject is used to find "running programs" and creates a "handle" to the object. (object or instance) If the program is not running it will create a new object. The second example I posted is right out of VB6 and does all the proper error checking. It tests to see if Excel is running and if it is it assigns an object name to it. (MyXL) The only "problem" with the code is it will not find more than 1 instance. Or perhaps I should say it stops after finding the first instance. The code I pointed to with the previous link will find all running instances of Excel and if you un-comment the optional line it will find all running programs on the machine. I'm not sure what your programing level is so please don't take this the wrong way. If you open 2 notepad.exe "sessions" they are considered seperate instances. That is they run in the own seperate "memory space". Excel fights most attempts at this. Probably because the designers figured it uses a lot of memory and other resources. Excel is a MDI or Mulitpul Document Interface where you can have more than one workbook open in 1 Excel instance and they can share resources. The only way I know of to get a seperate instance of Excel is to open Excel from the Startrun button or using VBA's Shell command to do the same. Shell "start " & path & filename -- John johnf 202 at hotmail dot com "Mark Grimes" wrote in message ... | Hi John, | | I think that we're at cross-purposes here. | | The code that is run does not create the other instances of Excel, | therefore your example needs to remove the GetObject() calls. | | The code needs to find the Excel objects that were created by other applications or | by someone opening up an instances of Excel. | | Any ideas much appreciated. | | Cheers, | Mark. |
All times are GMT +1. The time now is 12:03 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com