dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
I have a few examples of how to check if a 'saved workbook' of any kind is 'open' or 'exists' via checking the filepath; however, what can be done if there is no saved file but an open template?
What I'm wondering: Is there a method using VBA to check if a *.xltm file has been opened by double-click?
For instance, ambiguity would probably arise if the user went through the following process:
Workbook.xltm-->double click-->Workbook1-->use wb-->Close Workbook1
Re-open workbook.xltm-->Workbook2-->etc
My worry is that the user will use the Workbook twice or more in one day and any VBA references I use to point to the opened template file will error (in particular, copy/paste macros).
What I'm looking to do is check what the filename is first before forming a reference towards it:
e.g.,
Check if filename = "Workbook" & x
i.e., were x loops from 1 to 5
I'm wondering if it is possible to check each potential workbook name to see if it's open, find the value of x and use that to reference the open template file?
Would anyone be willing to advise of a way of doing this please?
Kind regards,
Doug.
I have a few examples of how to check if a 'saved workbook' of any kind is 'open' or 'exists' via checking the filepath; however, what can be done if there is no saved file but an open template?
What I'm wondering: Is there a method using VBA to check if a *.xltm file has been opened by double-click?
For instance, ambiguity would probably arise if the user went through the following process:
Workbook.xltm-->double click-->Workbook1-->use wb-->Close Workbook1
Re-open workbook.xltm-->Workbook2-->etc
My worry is that the user will use the Workbook twice or more in one day and any VBA references I use to point to the opened template file will error (in particular, copy/paste macros).
What I'm looking to do is check what the filename is first before forming a reference towards it:
e.g.,
Check if filename = "Workbook" & x
i.e., were x loops from 1 to 5
I'm wondering if it is possible to check each potential workbook name to see if it's open, find the value of x and use that to reference the open template file?
Would anyone be willing to advise of a way of doing this please?
Kind regards,
Doug.
VBA Code:
Public Function FileInUse(sFileName) As Boolean
On Error Resume Next
Open sFileName For Binary Access Read Lock Read As #1
Close #1
FileInUse = IIf(Err.Number > 0, True, False)
On Error GoTo 0
End Function
Sub Test_Sub()
myFilePath = "C:\Users\UserName\Desktop\example.xlsx"
If FileInUse(myFilePath) Then
MsgBox "File is Opened"
Else
MsgBox "File is Closed"
End If
End Sub