Outlook VBA Check if File is Open/Not Open

Felix_Dragonhammer

Board Regular
Joined
Apr 7, 2015
Messages
117
So I have a bit of code to check whether or not a file is open or not and act accordingly:

Code:
Sub Check()
    
    Dim Ret
    Ret = IsWorkBookOpen("C:\Users\sberger\Desktop\GPAU\Target.xlsx")
    If Ret = False Then
    
        Dim xlApp As Object
        Dim sourceWB As Object
    
        Set xlApp = CreateObject("Excel.Application")
        With xlApp
            .Visible = True
            .EnableEvents = False
        End With
    
           Set sourceWB = xlApp.Workbooks.Open("C:\Users\sberger\Desktop\GPAU\Target.xlsm", , False, , , , , , , True)
           sourceWB.Activate
           
    Else
    
        Exit Sub
    
    End If
     
    xlApp.Run ("Test")
    
End Sub

------------------------------------------------------------------------------------------------------------

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
    
    Select Case ErrNo
    Case 70:   IsWorkBookOpen = True
    Case Else: IsWorkBookOpen = False
    
    End Select
End Function

Much to my chagrin, I have discovered it is not. The workbook I have it checking is opened, but the code I have insists on opening a new one each time I run it. I can only assume I goofed up the function somehow when I adapted it.

Any help resolving this would be sincerely appreciated! Also, if anyone could provide code to activate the "Target" workbook in the event it is already open, I would appreciate that as well.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You mean the sub? Yeah, I thought that might have been the problem, so I checked that 4 or 5 times. I didn't detect any differences.
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,931
Members
449,480
Latest member
yesitisasport

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