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.