+ Reply to Thread
Results 1 to 3 of 3

when use vba to open file, How to notify the user when the excel are opened by other

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-18-2014
    Location
    hk
    MS-Off Ver
    Excel 365
    Posts
    105

    Smile when use vba to open file, How to notify the user when the excel are opened by other

    Hi all,

    I use the vba to open excel file. like workbooks.open filename:=abc.xlsm
    but I found that if the file is opened by other. Excel still open the file but it will not notify user that it is opened as read only.

    Would you add more line to notify user if the file is opened by other?

    Many thanks.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: when use vba to open file, How to notify the user when the excel are opened by other

    This macro will notify you whether or not the workbook is open.
    Sub Test()
        Dim WB As Workbook
        On Error Resume Next
        Set WB = Workbooks("abc.xlsm")
        If WB Is Nothing Then
            MsgBox "The workbook abc.xlsm is not open."
            Set WB = Nothing
            On Error GoTo 0
        Else
            MsgBox "The workbook abc.xlsm is open."
            Set WB = Nothing
            On Error GoTo 0
        End If
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: when use vba to open file, How to notify the user when the excel are opened by other

    Hi,

    Check the below code and hope it will help you :


    Sub TestFileOpened()
    
        ' Test to see if the file is open.
        If IsFileOpen("D:\Book2.xlsx") Then
            MsgBox "File already in use!"
        Else
            ' Display a message stating the file is not in use.
            MsgBox "File not in use!"
        End If
    
    End Sub
    
    Function IsFileOpen(filename As String)
        Dim filenum As Integer, errnum As Integer
    
        On Error Resume Next
        filenum = FreeFile()
        Open filename For Input Lock Read As #filenum
        Close filenum          ' Close the file.
        errnum = Err           ' Save the error number that occurred.
        On Error GoTo 0        ' Turn error checking back on.
    
        Select Case errnum
    
            Case 0
             IsFileOpen = False
    
            Case 70
                IsFileOpen = True
    
            Case Else
                Error errnum
        End Select
    
    End Function


    Regards,
    Paresh J
    Click on "* Add Reputation" as a way to say thanks

+ 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. can't open a new excel file while userform is opened
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-16-2013, 02:38 PM
  2. Notify user that a file is locked upon running insert/update query
    By Snoopy2003 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-21-2012, 04:59 PM
  3. Replies: 1
    Last Post: 12-13-2005, 12:45 PM
  4. Notify when file is open?
    By Rbp9ad in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-15-2005, 01:10 PM
  5. Replies: 2
    Last Post: 08-28-2005, 12:05 PM

Tags for this Thread

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