vba Open excel when File is used by another user

后端 未结 3 659
我在风中等你
我在风中等你 2021-01-26 06:16

This is my current code

Public Sub OpenFiles()
    \'Set LiveDealSheet file path
    \'Check if LiveDealSheet is already open
    LDSP = \"C:\\Users\\DCHEUNG\\De         


        
相关标签:
3条回答
  • 2021-01-26 07:01

    First of all thanks for you input. I have solve the problem on my own with some trial and error.

    changed the code to the following

    Public Sub OpenFiles()
        'Set LiveDealSheet file path
        'Check if LiveDealSheet is already open
        LDSP = "Z:\LiveDealSheet.xlsm"
        IsOTF = IsWorkBookOpen(LDSP)
    
        'Set quick workbook shortcut
        Set TWB = ThisWorkbook
        If IsOTF = False Then
            Set LDS = Workbooks.Open(LDSP)
            Debug.Print "Stage 1 Success"
    

    changed everything in this else statement

        Else
            On Error Resume Next
            Set LDS = Workbooks("LiveDealSheet.xlsm")
            If LDS Is Nothing Then Workbooks.Open FileName:=LDSP, ReadOnly:=True, IgnoreReadOnlyRecommended:=True
        End If
    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
        On Error GoTo 0
    
        Select Case ErrNo
        Case 0:    IsWorkBookOpen = False
        Case 70:   IsWorkBookOpen = True
        Case Else: Error ErrNo
        End Select
    End Function
    
    0 讨论(0)
  • 2021-01-26 07:01

    I would replace all the scripted above with this:

    Public Sub OpenFiles()
            On Error GoTo not_open
            Workbooks("C:\Users\DCHEUNG\Desktop\Programing\LiveDealSheet.xlsm").Activate
            Exit Sub
            not_open:
            Workbooks.Open FileName:="C:\Users\DCHEUNG\Desktop\Programing\LiveDealSheet.xlsm", ReadOnly:=True
            Err.Clear
            Resume Next
    End Sub
    
    0 讨论(0)
  • 2021-01-26 07:02

    I had the same issue and was helped somewhat by the existing posts, here. However, there was still a gap between the recommendations and reality. So, I'll try to share my lessons learned.

    In my case, I needed Workbooks.Open to open the most recent file in a shared folder. This file is often referenced by other users and is therefore frequently open by other users. Below is my first pass to give the VBA code "permission" to open the file as "read only."

    ' OPEN SOURCE-FILE IN READ-ONLY MODE (argument key below)
    Workbooks.Open _
        Filename:=strFilename, _
        UpdateLinks:=0, _
        ReadOnly:=True, _
        IgnoreReadOnlyRecommended:=True, _
        Notify:=True
    

    This actually works EXCEPT for when excel creates a temporary file in the source folder (the temp file will, therefore, always be the newest file in the folder). To handle that exception, I needed to truncate the temp characters: "~$". I have done that with

    Right([your_string], integer_length_of_string)
    

    See in context below.

    For Each objFile In myFolder.Files
        If InStr(1, objFile.Name, ".xlsm") And objFile.DateLastModified > dateFile Then
            dateFile = objFile.DateLastModified
            windowName = objFile.Name
    
                If InStr(1, windowName, "~$") Then
                    fileNameLen = Len(objFile.Name) - 2
                    windowName = Right(objFile.Name, fileNameLen)
                    strFilename = myDir & "\" & windowName
                End If
    
            strFilename = myDir & "\" & windowName
        End If
    Next objFile
    
    0 讨论(0)
提交回复
热议问题