+ Reply to Thread
Results 1 to 3 of 3

Check if file is open on network without opening it

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Moscow
    MS-Off Ver
    Excel 2010
    Posts
    291

    Check if file is open on network without opening it

    Hi all! Please help!

    Well, the problem is straighforward. Hope you can help. Found nothing really working with google.

    Many thanks in advance!

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Check if file is open on network without opening it

    Hi, Alexander_Golinsky,

    adapt the path and the name to suit:
    Sub TestFileOpen()
       Dim iOpen As Integer
       Dim sFile As String
       Dim strPath As String
       
       strPath = "C:\Temp\"
       sFile = "your workbook.xlsm"
       If strPath & sFile = "" Then Exit Sub
       iOpen = TestOpen(strPath & sFile)
       Select Case iOpen
          Case 0: MsgBox "Workbook " & strPath & sFile & " is free"
          Case 1: MsgBox "The file is locked by " & GetFileOwner(strPath, sFile) & "."
          Case 2: MsgBox "Workbook " & strPath & sFile & " wasn´t found"
       End Select
    End Sub
    Private Function TestOpen(sPath As String) As Integer
       If Dir(sPath) = "" Then
          TestOpen = 2
       Else
          On Error GoTo ERRORHANDLER
          Open sPath For Random Access Read Lock Read Write As #1
          Close #1
       End If
    ERRORHANDLER:
       If Err = 70 Then TestOpen = 1
    End Function
    Function GetFileOwner(fileDir As String, fileName As String) As String
    
    'On Error Resume Next
    Dim secUtil As Object
    Dim secDesc As Object
    Set secUtil = CreateObject("ADsSecurityUtility")
    Set secDesc = secUtil.GetSecurityDescriptor(fileDir & fileName, 1, 1)
    GetFileOwner = secDesc.owner
    End Function
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Moscow
    MS-Off Ver
    Excel 2010
    Posts
    291

    Re: Check if file is open on network without opening it

    HaHobe, Thanks!

    Open "F:\ANALIS\FIXD_INC\PRODUCTS\FI_Daily\Bondnames_FID.xlsm" For Random Access Read Lock Read Write As #1
    is what i looked for. I supose technically it's still opening but it's fast enough form me! Great!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Opening an Excel file from network
    By Tatoo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-28-2009, 12:40 PM
  2. Help Opening file on network
    By Mikeice in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-22-2005, 05:25 AM
  3. [SOLVED] How to check for an open network connection?
    By vrk1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-20-2005, 02:05 PM
  4. How to check for an open Network connection in Excel VBA?
    By vrk1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-17-2005, 10:05 PM
  5. [SOLVED] Opening a file in a macro (network)
    By David P. in forum Excel General
    Replies: 3
    Last Post: 06-08-2005, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1