Determining if a File Is Open, and Handling Appropriately

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I'm trying to write some code that will identify if a certain file is already open. If it is, then I want a msg box to appear. If it isn't, then I want some other code to run. I did some searching, and found some code that looks like it would work. I implemented that coding into my application, but it's not catching the open file. I'm not sure where I went wrong here.

Code:
Function IsWorkBookOpen(Name As String) As Boolean
    
    Dim xWB As Workbook
    
    On Error Resume Next
    
    Set xWB = Application.Workbooks.Item(Name)
    
    IsWorkBookOpen = (Not xWB Is Nothing)
End Function
Private Sub cmd_Export_6827_Click()
Dim xRet As Boolean
xRet = IsWorkBookOpen("6827_BKR371.xls*")
If xRet Then
    MsgBox ("Please ensure that all 6827_BKR371 files have been saved, and closed before proceeding.")
Else
    Call Export_6827_BKR371
    Unload Me
End If
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try...

Code:
Private Sub cmd_Export_6827_Click()
    Dim wb As Workbook
    For Each wb In Application.Workbooks
        If UCase(Left(wb.Name, 15)) = "6827_BKR371.XLS" Then
            Exit For
        End If
    Next wb
    If wb Is Nothing Then
        Call Export_6827_BKR371
        Unload Me
    Else
        MsgBox ("Please ensure that all 6827_BKR371 files have been saved, and closed before proceeding.")
    End If
End Sub

Hope this helps!
 
Upvote 0
@Domenic for some reason, it's still not recognizing that there is an open file with the naming convention.
 
Upvote 0
Have you confirmed that you do in fact have an open workbook whose first 15 characters start with "6827_BKR371.XLS" ?

If so, is the workbook opened in a separate and new instance of Excel?
 
Upvote 0
Yes, there is an open file with tat naming convention. It does appear that the open file is in a separate instance of Excel.
 
Upvote 0
In that case, I think you'll need to first loop through each instance of Excel, and then loop through each workbook within the Workbooks collection for the instance. Unfortunately, I don't have the code for doing such a task. Try searching Google for "how to loop through each instance of excel using vba". Actually, I did a quick search and found this link...

https://www.ozgrid.com/forum/forum/...-all-open-workbooks-in-all-instances-of-excel

I haven't tested it, but hopefully you'll be able to adapt it for your needs.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top