Loop Through Processes

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
I have a need to loop through every workbook open without closing them. (From Access) I was able to do that fairly easily with a for each workbook in Excel.Workbooks setup. However I realized that if there is more than one Excel Process open, I am only looping through the first Excel.Application object returned. Which means I am not truely checking everything that could be open. Which leads to the question... How do you loop through all workbooks in all Excel processes? I tried WMI but I could not figure out how to coerce a WMI Process into an Excel Application object. I'm Stumped.
 
Thank you Jaafar for this very useful code. For those who, as in my company, have 32 bit versions of Office installed on 64 bit Windows you will need to change the VBA7 compiler constant to Win64. Until I did that Excel kept on crashing because it was trying to use the wrong variable types, etc. So e.g. change #If VBA7 Then... to #If Win64 Then...

 
Upvote 0
Thank you Jaafar for this very useful code. For those who, as in my company, have 32 bit versions of Office installed on 64 bit Windows you will need to change the VBA7 compiler constant to Win64. Until I did that Excel kept on crashing because it was trying to use the wrong variable types, etc. So e.g. change #If VBA7 Then... to #If Win64 Then...

Thanks for letting us know.
 
Upvote 0
Correction: In working with this further, I now understand that the only change needed to the code for Excel 32 bit running on 64 bit Windows, are the pointer location constants. The rest of Jaafar's code with the VBA7 compiler constants are good as-is, just change the constants section to:

#If Win64 Then
Private Const vtbl_EnumRunning_Offset As Long = 9 * 4 * 2
Private Const vtbl_EnumMoniker_Next_Offset As Long = 3 * 4 * 2
Private Const vtbl_Moniker_GetDisplayName_offset As Long = 20 * 4 * 2
#Else
Private Const vtbl_EnumRunning_Offset As Long = 9 * 4
Private Const vtbl_EnumMoniker_Next_Offset As Long = 3 * 4
Private Const vtbl_Moniker_GetDisplayName_offset As Long = 20 * 4
#End If

I did still, despite the change, have one very old add-in (XNumbers / XNumb56) that kept on crashing Excel when I ran the routine, no matter what I did. I don't actually use that any more, so just removed it and now all OK.
 
Upvote 0
Correction: In working with this further, I now understand that the only change needed to the code for Excel 32 bit running on 64 bit Windows, are the pointer location constants. The rest of Jaafar's code with the VBA7 compiler constants are good as-is, just change the constants section to:

#If Win64 Then
Private Const vtbl_EnumRunning_Offset As Long = 9 * 4 * 2
Private Const vtbl_EnumMoniker_Next_Offset As Long = 3 * 4 * 2
Private Const vtbl_Moniker_GetDisplayName_offset As Long = 20 * 4 * 2
#Else
Private Const vtbl_EnumRunning_Offset As Long = 9 * 4
Private Const vtbl_EnumMoniker_Next_Offset As Long = 3 * 4
Private Const vtbl_Moniker_GetDisplayName_offset As Long = 20 * 4
#End If

I did still, despite the change, have one very old add-in (XNumbers / XNumb56) that kept on crashing Excel when I ran the routine, no matter what I did. I don't actually use that any more, so just removed it and now all OK.

Good to know .

Thank you .
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top