Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 945033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:41:21+00:00 2026-05-15T22:41:21+00:00

Can an Excel VBA macro, running in one instance of Excel, access the workbooks

  • 0

Can an Excel VBA macro, running in one instance of Excel, access the workbooks of another running instance of Excel? For example, I would like to create a list of all workbooks that are open in any running instance of Excel.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:41:21+00:00Added an answer on May 15, 2026 at 10:41 pm

    Cornelius’ answer is partially correct. His code gets the current instance and then makes a new instance. GetObject only ever gets the first instance, no matter how many instances are available. The question I believe is how can you get a specific instance from among many instances.

    For a VBA project, make two modules, one code module, and the other as a form with one command button named Command1. You might need to add a reference to Microsoft.Excel.

    This code displays all the name of each workbook for each running instance of Excel in the Immediate window.

    '------------- Code Module --------------
    
    Option Explicit
    
    Declare Function FindWindowEx Lib "User32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Declare Function GetClassName Lib "User32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Declare Function IIDFromString Lib "ole32" (ByVal lpsz As Long, ByRef lpiid As UUID) As Long
    Declare Function AccessibleObjectFromWindow Lib "oleacc" (ByVal hWnd As Long, ByVal dwId As Long, ByRef riid As UUID, ByRef ppvObject As Object) As Long
    
    Type UUID 'GUID
      Data1 As Long
      Data2 As Integer
      Data3 As Integer
      Data4(7) As Byte
    End Type
    
    '------------- Form Module --------------
    
    Option Explicit
    
    Const IID_IDispatch As String = "{00020400-0000-0000-C000-000000000046}"
    Const OBJID_NATIVEOM As Long = &HFFFFFFF0
    
    'Sub GetAllWorkbookWindowNames()
    Sub Command1_Click()
        On Error GoTo MyErrorHandler
    
        Dim hWndMain As Long
        hWndMain = FindWindowEx(0&, 0&, "XLMAIN", vbNullString)
    
        Do While hWndMain <> 0
            GetWbkWindows hWndMain
            hWndMain = FindWindowEx(0&, hWndMain, "XLMAIN", vbNullString)
        Loop
    
        Exit Sub
    
    MyErrorHandler:
        MsgBox "GetAllWorkbookWindowNames" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Sub
    
    Private Sub GetWbkWindows(ByVal hWndMain As Long)
        On Error GoTo MyErrorHandler
    
        Dim hWndDesk As Long
        hWndDesk = FindWindowEx(hWndMain, 0&, "XLDESK", vbNullString)
    
        If hWndDesk <> 0 Then
            Dim hWnd As Long
            hWnd = FindWindowEx(hWndDesk, 0, vbNullString, vbNullString)
    
            Dim strText As String
            Dim lngRet As Long
            Do While hWnd <> 0
                strText = String$(100, Chr$(0))
                lngRet = GetClassName(hWnd, strText, 100)
    
                If Left$(strText, lngRet) = "EXCEL7" Then
                    GetExcelObjectFromHwnd hWnd
                    Exit Sub
                End If
    
                hWnd = FindWindowEx(hWndDesk, hWnd, vbNullString, vbNullString)
                Loop
    
            On Error Resume Next
        End If
    
        Exit Sub
    
    MyErrorHandler:
        MsgBox "GetWbkWindows" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Sub
    
    Public Function GetExcelObjectFromHwnd(ByVal hWnd As Long) As Boolean
        On Error GoTo MyErrorHandler
    
        Dim fOk As Boolean
        fOk = False
    
        Dim iid As UUID
        Call IIDFromString(StrPtr(IID_IDispatch), iid)
    
        Dim obj As Object
        If AccessibleObjectFromWindow(hWnd, OBJID_NATIVEOM, iid, obj) = 0 Then 'S_OK
            Dim objApp As Excel.Application
            Set objApp = obj.Application
            Debug.Print objApp.Workbooks(1).Name
    
            Dim myWorksheet As Worksheet
            For Each myWorksheet In objApp.Workbooks(1).Worksheets
                Debug.Print "     " & myWorksheet.Name
                DoEvents
            Next
    
            fOk = True
        End If
    
        GetExcelObjectFromHwnd = fOk
    
        Exit Function
    
    MyErrorHandler:
        MsgBox "GetExcelObjectFromHwnd" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VBA macro that uses Microsoft MapPoint to calculate the distance between
I' trying to write an xml file from Excel VBA using Microsoft XML 6.0.
I have an Excel sheet with cells containing html. How can I batch convert
How to check from .net code whether Trust access to the VBA project object
I'm trying to compare two Excel files and store what's only there in the
I'm trying to build a VBA form that runs a SQL Server stored procedure
I have a workbook where several of my data feeds pass a parameter back
I'm seeking help to push in value parse from XML based on certain filter/word
I want to save a range of cells in XLS sheet as a HTML
I cannot use Cells(x,y) anymore in OpenOffice (seems to be a bug, from what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.