+ Reply to Thread
Results 1 to 7 of 7

Excel 2010 VBA - find name of the user of an open excel file

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2012
    Location
    Cedar Rapids, Iowa
    MS-Off Ver
    Excel 2012
    Posts
    5

    Excel 2010 VBA - find name of the user of an open excel file

    Hello All,
    I have looked at many scripts that look at usernames of open workbooks on my computer, but the files reside on a server. I have a gui that users will open up, and it will display all the excel files and their status: open, closed, open with read/write/ open with read only. My next step is to have the name of the individual also displayed on those that are read/write. Issue is I have people internationally and if they forget to close the excel sheet then nobody else can use it as you already know. I want people to be able to know who has it open so they can contact them if there is a need to swap users.

    I also want (maybe a not possible) to be able to know how long the file has been open. This way the users will be able to know if the actually user has forgot they have it open (happens a lot) and needs someone to let them know its time to share.

    Appreciate any help I can get

    Thanks

    Richard
    Last edited by rjhanson; 10-09-2013 at 11:15 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,006

    Re: Excel 2010 VBA - find name of the user of an open excel file

    If the workbook resides on a central server somewhere, and if it is macro enabled, you could write a text file to a fixed location with the user's log in ID, and the time the file was opened.

    You could then interrogate the text file. In fact, you could probably append to it so you'd have a history of who uses the file.

    Just an idea ...


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-17-2012
    Location
    Cedar Rapids, Iowa
    MS-Off Ver
    Excel 2012
    Posts
    5

    Re: Excel 2010 VBA - find name of the user of an open excel file

    Quote Originally Posted by TMShucks View Post
    If the workbook resides on a central server somewhere, and if it is macro enabled, you could write a text file to a fixed location with the user's log in ID, and the time the file was opened.

    You could then interrogate the text file. In fact, you could probably append to it so you'd have a history of who uses the file.

    Just an idea ...


    Regards, TMS
    Thanks TMS, I like the Idea, greatly appreciate it

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    45,006

    Re: Excel 2010 VBA - find name of the user of an open excel file

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  5. #5
    Registered User
    Join Date
    08-17-2012
    Location
    Cedar Rapids, Iowa
    MS-Off Ver
    Excel 2012
    Posts
    5

    Re: Excel 2010 VBA - find name of the user of an open excel file

    Quote Originally Posted by TMShucks View Post
    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.
    Thanks TMS, but I will wait a little longer as I am hoping someone will be able to figure out how to directly from my open workbook to get the user's information on a workbook someone else has open on the server. I am working on your idea now and then if someone comes up with the solution I really want I will close it.

    Thanks for all the help

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Excel 2010 VBA - find name of the user of an open excel file

    I suppose you could use something like:
    MsgBox "Open since " & CreateObject("scripting.filesystemobject").getfile("E:\Book1.xls").datelastaccessed
    to find out how long it has been opened. I'm not sure how to get the user but I'll have a think.

    Edit: According to google:
    Option Explicit
    
    Sub macro_1()
    MsgBox "Open since " & CreateObject("scripting.filesystemobject").getfile("E:\Book1.xls").datelastaccessed & " by " & LastUser("E:\Book1.xls")
    End Sub
    Private Function LastUser(strPath As String) As String
    '// Code by Helen from http://www.visualbasicforum.com/index.php?s=
    '// This routine gets the Username of the File In Use
    '// Credit goes to Helen for code & Mark for the idea
    '// Insomniac for xl97 inStrRev
    '// Amendment 25th June 2004 by IFM
    '// : Name changes will show old setting
    '// : you need to get the Len of the Name stored just before
    '// : the double Padded Nullstrings
    Dim strXl As String
    Dim strFlag1 As String, strflag2 As String
    Dim i As Integer, j As Integer
    Dim hdlFile As Long
    Dim lNameLen As Byte
    
    
    strFlag1 = Chr(0) & Chr(0)
    strflag2 = Chr(32) & Chr(32)
    
    hdlFile = FreeFile
    Open strPath For Binary As #hdlFile
        strXl = Space(LOF(hdlFile))
        Get 1, , strXl
    Close #hdlFile
    
    j = InStr(1, strXl, strflag2)
    
    #If Not VBA6 Then
        '// Xl97
        For i = j - 1 To 1 Step -1
            If Mid(strXl, i, 1) = Chr(0) Then Exit For
        Next
        i = i + 1
    #Else
        '// Xl2000+
        i = InStrRev(strXl, strFlag1, j) + Len(strFlag1)
    #End If
    
    '// IFM
    lNameLen = Asc(Mid(strXl, i - 3, 1))
    LastUser = Mid(strXl, i, lNameLen)
    
    End Function
    then you probably want something along the lines of:
    Option Explicit
    
    Function FileLocked(strFileName As String) As Boolean
       On Error Resume Next
       ' If the file is already opened by another process,
       ' and the specified type of access is not allowed,
       ' the Open operation fails and an error occurs.
       Open strFileName For Binary Access Read Write Lock Read Write As #1
       Close #1
       ' If an error occurs, the document is currently open.
       If Err.Number <> 0 Then
          ' Display the error number and description.
          MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
          FileLocked = True
          Err.Clear
       End If
    End Function
    
    Sub test()
        Debug.Print FileLocked("C:\Users\ooo\Desktop\test.xlsx")
    
    End Sub
    to see if the file is locked
    Last edited by ragulduy; 10-09-2013 at 09:37 AM.

  7. #7
    Registered User
    Join Date
    08-17-2012
    Location
    Cedar Rapids, Iowa
    MS-Off Ver
    Excel 2012
    Posts
    5

    Re: Excel 2010 VBA - find name of the user of an open excel file

    Quote Originally Posted by yudlugar View Post
    I suppose you could use something like:
    MsgBox "Open since " & CreateObject("scripting.filesystemobject").getfile("E:\Book1.xls").datelastaccessed
    to find out how long it has been opened. I'm not sure how to get the user but I'll have a think.

    Edit: According to google:
    Option Explicit
    
    Sub macro_1()
    MsgBox "Open since " & CreateObject("scripting.filesystemobject").getfile("E:\Book1.xls").datelastaccessed & " by " & LastUser("E:\Book1.xls")
    End Sub
    Private Function LastUser(strPath As String) As String
    '// Code by Helen from http://www.visualbasicforum.com/index.php?s=
    '// This routine gets the Username of the File In Use
    '// Credit goes to Helen for code & Mark for the idea
    '// Insomniac for xl97 inStrRev
    '// Amendment 25th June 2004 by IFM
    '// : Name changes will show old setting
    '// : you need to get the Len of the Name stored just before
    '// : the double Padded Nullstrings
    Dim strXl As String
    Dim strFlag1 As String, strflag2 As String
    Dim i As Integer, j As Integer
    Dim hdlFile As Long
    Dim lNameLen As Byte
    
    
    strFlag1 = Chr(0) & Chr(0)
    strflag2 = Chr(32) & Chr(32)
    
    hdlFile = FreeFile
    Open strPath For Binary As #hdlFile
        strXl = Space(LOF(hdlFile))
        Get 1, , strXl
    Close #hdlFile
    
    j = InStr(1, strXl, strflag2)
    
    #If Not VBA6 Then
        '// Xl97
        For i = j - 1 To 1 Step -1
            If Mid(strXl, i, 1) = Chr(0) Then Exit For
        Next
        i = i + 1
    #Else
        '// Xl2000+
        i = InStrRev(strXl, strFlag1, j) + Len(strFlag1)
    #End If
    
    '// IFM
    lNameLen = Asc(Mid(strXl, i - 3, 1))
    LastUser = Mid(strXl, i, lNameLen)
    
    End Function
    then you probably want something along the lines of:
    Option Explicit
    
    Function FileLocked(strFileName As String) As Boolean
       On Error Resume Next
       ' If the file is already opened by another process,
       ' and the specified type of access is not allowed,
       ' the Open operation fails and an error occurs.
       Open strFileName For Binary Access Read Write Lock Read Write As #1
       Close #1
       ' If an error occurs, the document is currently open.
       If Err.Number <> 0 Then
          ' Display the error number and description.
          MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
          FileLocked = True
          Err.Clear
       End If
    End Function
    
    Sub test()
        Debug.Print FileLocked("C:\Users\ooo\Desktop\test.xlsx")
    
    End Sub
    to see if the file is locked
    0

    Hello yudlugar,
    I tried the above scripts but did not have any good luck with it. However, I found this from Jeff Grove that works very well for me

    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


    Thanks everyone for all the help

+ 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. Replies: 1
    Last Post: 05-25-2013, 02:11 AM
  2. Excel 2010 open semi-colon separated file
    By thinksnowjob in forum Excel General
    Replies: 3
    Last Post: 01-15-2013, 08:21 PM
  3. Cannot Open .xls file created in Excel 2003 with Excel 2010
    By HammernNail in forum Excel General
    Replies: 2
    Last Post: 01-20-2012, 01:06 PM
  4. [SOLVED] Need to Save Excel XML as just Excel file for user to open in Excel 2000
    By Fatmosh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-20-2005, 01:05 PM
  5. Replies: 0
    Last Post: 09-19-2005, 03:05 PM

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