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

  • SEARCH
  • Home
  • 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 8165971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:41:56+00:00 2026-06-06T19:41:56+00:00

This continues from a previous question . I tried the suggested fix to check

  • 0

This continues from a previous question. I tried the suggested fix to check if an Excel file is open locally from an Outlook macro (Office 2010).

Public Sub UpdateFileIndex(ByVal FullFilePath As String, ByVal DocNo As String)
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.WorkSheet
    
    On Error Resume Next
    Set xlApp = GetObject(FullFilePath).Application
    Debug.Print "Error = " & Err

    If Err.Number = 0 Then ' Workbook is open locally
        ' Do stuff
    ElseIf Err.Number = 429 Then ' Workbook is not open locally
        ' Do different stuff
    End If

    ' Do a bunch of other stuff
End Sub

Now for open or closed files given by FullFilePath (e.g. "C:\Data\Data.xlsx"):

  • Set xlApp = GetObject(FullFilePath).Application

gives me 0 error either way. (i.e. it opens the file if it’s not open.)

  • Set xlApp = GetObject(Dir(FullFilePath)).Application

gives me -214722120 for both cases. (Automation error)

  • Set xlApp = GetObject(, "Excel.Application")

gives me 0 when open and 429 when not open. See below.

  • Set xlApp = GetObject(Dir(FullFilePath), "Excel.Application")

gives me 432 for both cases. (File name or class name not found during Automation operation)

  • Set xlApp = GetObject(FullFilePath, "Excel.Application")

gives me 432 for both cases.

So the only case that works is the initially suggested fix (see link at top), which cannot find the file unless it’s in the first instance of Excel open locally, which may not always be the case (i.e. it may be open in a second instance).

Ultimately I’d like to check if the file is open on the network, and if it is check if it’s open locally.

  • 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-06-06T19:41:59+00:00Added an answer on June 6, 2026 at 7:41 pm

    If you have multiple Excel instances open then this is what I suggest.

    Logic

    1. Check if your workbook is open or not. If not open, then open it.
    2. If it is open then it could be in any Excel instance.
    3. Find the Excel instance and bind with the relevant workbook.

    GetObject unfortunately will return the same instance every time unless you close that Excel instance. Also there is no reliable way to get it to loop through all Excel instances. Talking of reliability, I would turn your attention towards APIs. The 3 APIs that we will use is FindWindowEx , GetDesktopWindow and AccessibleObjectFromWindow&

    See this example (TRIED AND TESTED in EXCEL 2010)

    Option Explicit
    
    Private 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
    
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    
    Private Declare Function AccessibleObjectFromWindow& Lib "oleacc" _
    (ByVal hwnd&, ByVal dwId&, riid As GUID, xlWB As Object)
    
    Private Const OBJID_NATIVEOM = &HFFFFFFF0
    
    Private Type GUID
        lData1 As Long
        iData2 As Integer
        iData3 As Integer
        aBData4(0 To 7) As Byte
    End Type
    
    Sub Sample()
        Dim Ret
        Dim oXLApp As Object, wb As Object
        Dim sPath As String, sFileName As String, SFile As String, filewithoutExt As String
        Dim IDispatch As GUID
    
        sPath = "C:\Users\Chris\Desktop\"
        sFileName = "Data.xlsx": filewithoutExt = "Data"
        SFile = sPath & sFileName
    
        Ret = IsWorkBookOpen(SFile)
    
        '~~> If file is open
        If Ret = True Then
            Dim dsktpHwnd As Long, hwnd As Long, mWnd As Long, cWnd As Long
    
            SetIDispatch IDispatch
    
            dsktpHwnd = GetDesktopWindow
    
            hwnd = FindWindowEx(dsktpHwnd, 0&, "XLMAIN", vbNullString)
    
            mWnd = FindWindowEx(hwnd, 0&, "XLDESK", vbNullString)
    
            While mWnd <> 0 And cWnd = 0
                cWnd = FindWindowEx(mWnd, 0&, "EXCEL7", filewithoutExt)
                hwnd = FindWindowEx(dsktpHwnd, hwnd, "XLMAIN", vbNullString)
                mWnd = FindWindowEx(hwnd, 0&, "XLDESK", vbNullString)
            Wend
    
            '~~> We got the handle of the Excel instance which has the file
            If cWnd > 0 Then
                '~~> Bind with the Instance
                Call AccessibleObjectFromWindow(cWnd, OBJID_NATIVEOM, IDispatch, wb)
                '~~> Work with the file
                With wb.Application.Workbooks(sFileName)
                    '
                    '~~> Rest of the code
                    '
                End With
            End If
    
        '~~> If file is not open
        Else
            On Error Resume Next
            Set oXLApp = GetObject(, "Excel.Application")
    
            '~~> If not found then create new instance
            If Err.Number <> 0 Then
                Set oXLApp = CreateObject("Excel.Application")
            End If
            Err.Clear
            On Error GoTo 0
    
            Set wb = oXLApp.Workbooks.Open(SFile)
            '
            '~~> Rest of the code
            '
        End If
    End Sub
    
    Private Sub SetIDispatch(ByRef ID As GUID)
        With ID
            .lData1 = &H20400
            .iData2 = &H0
            .iData3 = &H0
            .aBData4(0) = &HC0
            .aBData4(1) = &H0
            .aBData4(2) = &H0
            .aBData4(3) = &H0
            .aBData4(4) = &H0
            .aBData4(5) = &H0
            .aBData4(6) = &H0
            .aBData4(7) = &H46
        End With
    End Sub
    
    '~~> Function to check if file is open
    Function IsWorkBookOpen(FileName As String)
        Dim ff As Long, ErrNo As Long
    
        On Error Resume Next
        ff = FreeFile()
        Open FileName For Input Lock Read As #ff
        Close ff
        ErrNo = Err
        On Error GoTo 0
    
        Select Case ErrNo
        Case 0:    IsWorkBookOpen = False
        Case 70:   IsWorkBookOpen = True
        Case Else: Error ErrNo
        End Select
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Leading on from a previous question FCM Clustering numeric data and csv/excel file Im
This is the continual question from this previous question I've asked: Change Database Column
This maybe a continuation from 2 of my previous question, or maybe it's a
And my confusion with JSF continues. This is a continuation of a question asked
This question is in continuation to my previous post located here . Since there
This is a follow up from this question, Lock Cells after Data Entry .
From a previous question , I have the following: So I have implemented a
Following on from a previous question I had here : Copying a string from
UPDATE 1/31/10 : Since this thread continues to get a lot of views...I am
I've been searching for answers for quite some time on this as it continues

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.