+ Reply to Thread
Results 1 to 8 of 8

Is there a way to use VBA to know a xlsm file is opened by other person?

  1. #1
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    782

    Is there a way to use VBA to know a xlsm file is opened by other person?

    Two files: a xlsm file with VBA code (filename: XYZ_application.xlsm), another xlsx file (filename: output.xlsx)

    There are around 20 employees in the team.

    XYZ_application.xlsm opens an input excel file, and also opens output.xlsx, then pull some data from the input excel file to output.xlsx, then uses another program (non VBA program) to import output.xlsx data into MS SQL database. People prefer to do it this way, there is hard code for file path of output.xlsx in import program.

    Ideally, both XYZ_application.xlsm and output.xlsx are closed before being used.

    But some people forget to close the files are using XYZ_application.xlsm and/or output.xlsx

    There will be issue if multiple people working on output.xlsx at the same time. There should be only one person using the program at a time(everyone tries to import his own data into MS SQL database), since it is not often used program, it is okay that only one person uses the program at a time. The bigger reason is: there is hard code for file path of output.xlsx in import program (written by former employee). We cannot have multiple people working on output.xlsx at the same time, moreover, there is VBA code in XYZ_application.xlsm to open output.xlsx, so output.xlsx cannot be already opened.

    Question: Is there a way to put some code in XYZ_application.xlsm Workbook_Open to detect if current file XYZ_application.xlsm is already opened by others and if output.xlsx is already opened. If it is possible, is there a way to know who keeps the file open?

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,146

    Re: Is there a way to use VBA to know a xlsm file is opened by other person?

    Two approaches ...

    #1 You could forgo trying to track who has opened the file/s but placing a timer in each workbook that automatically closes the workbook after "x" minutes.

    #2 You can hide a sheet in each workbook that tracks the computer name of each individual that accessed the file with a time stamp and date.

    There are plenty of samples on this forum and the internet with complete code for you to search for.

  3. #3
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,425

    Re: Is there a way to use VBA to know a xlsm file is opened by other person?

    The following function will check if the file is open.
    Please Login or Register  to view this content.
    Artik

  4. #4
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    782

    Re: Is there a way to use VBA to know a xlsm file is opened by other person?

    Quote Originally Posted by Artik View Post
    The following function will check if the file is open.
    Please Login or Register  to view this content.
    Artik
    The issue: if a file is already open, the second person may open xlsm file as read only and could not run any program.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,425

    Re: Is there a way to use VBA to know a xlsm file is opened by other person?

    The following code construction will not allow you to use a workbook that is opened as ReadOnly.

    In Thisworkbook module:
    Please Login or Register  to view this content.
    and in standard module (i.e. Module1):
    Please Login or Register  to view this content.
    Artik

  6. #6
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    782

    Re: Is there a way to use VBA to know a xlsm file is opened by other person?

    Quote Originally Posted by Artik View Post
    The following code construction will not allow you to use a workbook that is opened as ReadOnly.

    In Thisworkbook module:
    Please Login or Register  to view this content.
    and in standard module (i.e. Module1):
    Please Login or Register  to view this content.
    Artik
    Application.OnTime Now, "CloseMe"
    Sub CloseMe()

    Could you please explain more how these two lines work together?

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,425

    Re: Is there a way to use VBA to know a xlsm file is opened by other person?

    If the workbook is opened in read-only mode, the part of the procedure that calls the OnTime method to close the workbook will be executed. If the workbook is opened in full access mode the part of the procedure marked (...) will be executed. Although the OnTime method is defined to run immediately (Now() ), it will not execute immediately, but only after the Workbook_Open procedure has completed.
    In theory, you could put the command to close the workbook in Workbook_Open, instead of calling OnTime. However, life experience tells you to do it in a separate thread.

    Artik

  8. #8
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    782

    Re: Is there a way to use VBA to know a xlsm file is opened by other person?

    Thank you very much.

+ 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. Open .xlsm file once .csv is opened
    By elgato74 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-04-2024, 02:46 PM
  2. Show the Username of the Person who Opened A File as Read Only
    By richardking in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-19-2017, 02:00 PM
  3. [SOLVED] How to export from xlsm file to xls but original file must stay in xlsm(vba script)
    By martin81 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-03-2016, 09:51 AM
  4. Replies: 0
    Last Post: 01-24-2014, 09:10 AM
  5. Replies: 4
    Last Post: 01-09-2014, 03:13 PM
  6. Making distinction between a actual person or VBA opening a xlsm file
    By Renze in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-18-2013, 04:20 AM
  7. Replies: 3
    Last Post: 05-21-2013, 08:22 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