I copied some UDF function code that serves my purpose from this guide
which supposedly has worked for others. I didn't really alter the code much, but for transparency here is the exact code I have in my workbook as a module:
Function IsExtWorkBookOpen(Name As String) As Boolean
Dim xWb As Workbook
On Error Resume Next
Set xWb = Application.Workbooks.Item(Name)
IsExtWorkBookOpen = (Not xWb Is Nothing)
End Function
=IsExtWorkBookOpen("C:\Computer\Fake\filepath\name\CORRECTLY SPELLED FULL FILENAME.xlsm")
=IsExtWorkBookOpen("C:\Computer\Fake\filepath\name\[CORRECTLY SPELLED FULL FILENAME].xlsm")
=IsExtWorkBookOpen("[CORRECTLY SPELLED FULL FILENAME]")
Also tried adding the following as a second parameter:
Optional VolatileParameter As Variant
and calling , NOW() as the second parameter to see if it has something to do with the UDF needing to be volatile. To my dismay, nothing seems to work, as the resulting value in the cells is always #NAME.
It gets weird though, because I tested my code in the VBA editor by inserting a procedure, calling the UDF in the procedure, placing breakpoints in the UDF and running the procedure. Here's the code for that:
Public Sub Test1()
Dim x
x = IsExtWorkBookOpen("EXTERNAL WORKBOOK.xlsm")
MsgBox (x)
End Sub
The procedure runs perfectly, and returns a FALSE value. Can't notice anything off in the registries during breakpoints. Although I did notice it seems to return FALSE even if the external workbook I am checking is actually open... so thats probably a bigger problem than trying to figure out why the function won't work when entered into a cell.
Even weirder, is the very first time I entered the function into a cell, it did return and display FALSE! but only the very first time, ever since just #NAME.
I tried crawling the web for similar programming bugs and the closest I found was this forum thread. It seems that maybe, as with this user's UDF, mine is not even executing because excel doesn't know how to execute it from the start?
Finally, I noticed the following comment by 'Joe' on the first webpage, from where I copied the code, which stated:
Just want to make sure everyone is aware, the "IsWorkBookOpen" function will only be able to tell if a workbook is open in the current instance of Excel. If you have multiple instances open you would need to run it on each instance to be sure the workbook is (not) open, using this code alone.
Reading this comment, is this code doomed to work for my purpose from the beginning? I still doubt this because the #NAME error happens even when I tried calling the UDF with the correctly spelled filename of the current workbook (not the external one)(the workbook within which the VBA module is) which leads me to believe the bug is elsewhere in the code functionality.
If so though, can anyone give me a hint as to what code is needed to successfully perform my function that checks if another workbook is open in the same local PC/desktop?
EDIT: After further debugging/investigating, I discovered that the reason I can't get a simple function like that @FrankProp recommended to function for my purpose is because the external workbook I am trying to check as active (open) or not is always open in a different instance of excel. Probably because the external workbook is also a Macro-enabled workbook. So when I loop through Application.workbooks it only looks at other workbooks active in the same instance/application as the original workbook, which the workbook I'm checking for will never be in.
Reading through some other SOF threads, it seems that what I am trying to do is possible but is considerably more complicated.
I just tried adapting code from this thread for my purpose, but at first I got a runtime error 438 - VBA object doesn't support this property or method on line 41, Col 16 If xl Is acc.Application Then which I deduce, meant that an excel.application object had not been correctly assigned to the acc object so it couldn't access a non-existing .application property. The error only happens when the 2nd workbook, in a 2nd instance of excel is open/active. I'll keep updating this post with my progress. Also will change post title to be more relevant.