Doesn't recognize opened workbook

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
When I loop through the 2 open workbooks using vba it only recognizes one?

Code:
Dim xlWB As Excel.Workbook
Dim objExcelApp As Excel.Application

Set objExcelApp = GetObject(, "Excel.Application")

For Each xlWB In objExcelApp.Workbooks
    Debug.Print xlWB.Name
Next xlWB

Set objExcelApp = Nothing
Set xlWB = Nothing

One of the workbooks is a xlsm file and the other is a xlsx file. It doesn't recognize the xlsx file. Tried opening a separate xlsx file and it recognizes that.

If I have only the rogue xlsx open on it's own, then the code recognizes it!

I essentially want to work on 2 Excel files in my code.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Are the 2 files open in separate instances of Excel?
 
Upvote 0
When you loop through objExcelApp.Workbooks you will only return the workbook(s) open in the instance of Excel that objExcelApp refers to.
 
Upvote 0
OK I understand.
How do you refer to multiple workbooks that are already open in their respective Excel instances?
 
Upvote 0
Although two open workbooks appear in the task bar as separate icons, I always thought only one instance of Excel was actually running because if I minimize everything else, I can only find one Excel window. Furthermore, from one Excel workbook I can switch to the other workbook from the current one using the Window option on the View tab of the ribbon. That being said, I haven't worked with two open workbooks at the same time, but I'd suspect you'd have to declare them both then act on them individually. Like

Code:
Dim objExcelApp As Excel.Application
Dim xlWB1 As Excel.Workbook
Dim xlWB2 As Excel.Workbook

On Error GoTo errHandler

Set objExcelApp = GetObject(, "Excel.Application")
Set xlWB1 = pathToFirstWorkbook
Set xlWB2 = pathToOtherWorkbook

do stuff to each workbook

ExitHere:
Set xlWB1 = Nothing
Set xlWB2 = Nothing
Set objExcelApp = Nothing
Exit Sub

errHandler:
msgbox "Error " & Err.Number & ": " & Err.Description
Resume ExitHere
 
Upvote 0
Separate instances can be used by running Excel from the start menu twice (instead of, for instance, running Excel from the start menu once, then opening two workbooks).
 
Upvote 0
We are creatures of habit! I always use the icons in my secondary task bar and open additional books from the "recent documents" list. Thanks for a lesson from class 101!;)
Then from Access, using vba, would you open one instance and two workbooks or two instances? I see no advantage to doing the latter...
 
Upvote 0
Then from Access, using vba, would you open one instance and two workbooks or two instances? I see no advantage to doing the latter...
My preference would be to open one instance. But, alas, this wouldn't help the OP - that code is not opening any instances at all but rather using GetObject to hook an already running instance. I have no idea how you would tell it to get a second one and not a first one. Personally I have always avoided GetObject since it seems a little hard to be sure it's always working the way you want it to.
 
Upvote 0

Forum statistics

Threads
1,221,771
Messages
6,161,847
Members
451,723
Latest member
Rachetsely

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