+ Reply to Thread
Results 1 to 14 of 14

Macro to not allow opening of file only on certain computer if it is already opened

  1. #1
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Macro to not allow opening of file only on certain computer if it is already opened

    Good morning all;

    I have a computer that users are regularly opening a read only copy of a file because it is opened elsewhere. The users then proceed to edit the file. I need a macro that will not allow the excel file to be opened (at all) on that particular workstation if it is already opened by any other workstation. However, if the file is opened already, I do want other workstations to be able to open a read only copy of the file.

    Thank you

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Macro to not allow opening of file only on certain computer if it is already open

    You can put this sub in the open event of the workbook you want to restrict access to:

    Please Login or Register  to view this content.


    To get the name of the workstation, type this into the immediate window while on that workstation:

    Please Login or Register  to view this content.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    I copied/pasted this code into the Open event in the workbook and replaced the "workstationname" in quotes to the computer name of the computer that I don't want to be able to open a read only copy of the file. I compiled the code and saved the file.

    I then opened the workbook on my laptop and then tried to open it on the computer whose name was mentioned above. There was the usual message about someone having the file open, and the dialog box asked if I wanted to open a read only copy. I selected yes and it opened without further fanfare.

    I'm going to look further but did I mis-read your post and do something wrong? Doesn't seem that the file is "seeing" that code.

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Macro to not allow opening of file only on certain computer if it is already open

    Hmm, when I tested it using my comp and a coworker's comp, after I got the message about the file being in use, regardless of my choosing Notify or Read-Only, the macro ran and the workbook closed itself as expected. Did you leave the quotation marks around the workstation name? They do need to be there as it is a string.

  5. #5
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    I did leave the quotes. I had tried it without the quotes also. I am using Win7 and XP as test machines. I do get the notice that the file is in use, but can select the open read only option and the file opens.

    Here is a screen clip of the VBA code. Maybe you can see something wrong there?
    http://i686.photobucket.com/albums/v...xcel/Excel.jpg

  6. #6
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Macro to not allow opening of file only on certain computer if it is already open

    I don't know how enabling macro protection works in Excel 10. I'm using 2007. I know I had to enable macros before the workbook_open event would run. Did you enable macros or do you have a security setting low enough so they run atuomatically?

  7. #7
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    hmmm;

    I did have both computers Excel macro settings set to "Low" security, so that it runs any and all macros. But this solution is not a macro, right? I can set the macro security to "disable all macros with notification" and the file opens without any mention of a macro.

    In other files I've created macros in I would normally get a message about whether or not to allow the macros. I don't get any such warnings on this file.

    Is this supposed to be a macro? I just plugged in the code where you described earlier and compiled it and saved the file.

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Macro to not allow opening of file only on certain computer if it is already open

    The code is considered a macro. If you set the security to give you a notification when macros are detected, Excel should notify you. Any time you run vba code it is considered a macro (for our purposes). The fact that you are not getting a warning is confusing to me. Why not try adding a message box in the beginning of the procedure to make sure it is firing when the workbook opens?

  9. #9
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    well this has got me stumped as well. I've tried turning off macros, setting the various macro security levels, etc. I have it ask me to enable macros and when the file opens it asks to enable macros like it should.

    For whatever reason the macro just isn't firing. Is there anything you can think that I've missed here or is there a diag tool to see why the macro isn't firing?

  10. #10
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    Quote Originally Posted by davegugg View Post
    The code is considered a macro. If you set the security to give you a notification when macros are detected, Excel should notify you. Any time you run vba code it is considered a macro (for our purposes). The fact that you are not getting a warning is confusing to me. Why not try adding a message box in the beginning of the procedure to make sure it is firing when the workbook opens?
    I will try this.

    I also copied the file from my network location over to my local hard drive. Same "non" results. I'll let you know about the message box idea.

  11. #11
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    Dave;

    I created a message box and when I open the file, I DO see my message box open up so something is not quite right with how my system is interacting with the code. Can you take a look at the screenshot in my above post to make sure I plugged that code in where/how I should have?

  12. #12
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    Dan;

    I added msgboxes to all of the comments in the code. See below. The last message box that shows on the screen when the file is opened elsewhere is the one saying "Check if the computer opening the file is the workstation........" so it appears the code is not running past that point.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Macro to not allow opening of file only on certain computer if it is already open

    I would guess that the Computername is not being read correctly. It is case sensitive. It seems overkill, but did you copy the string you got directly from the immediate window into your code?

  14. #14
    Registered User
    Join Date
    03-03-2011
    Location
    Oregon
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Macro to not allow opening of file only on certain computer if it is already open

    Quote Originally Posted by davegugg View Post
    I would guess that the Computername is not being read correctly. It is case sensitive. It seems overkill, but did you copy the string you got directly from the immediate window into your code?
    That was the issue. Even though I knew how I had capitalized the computer's name, when I ran the code in the immediate window again I saw that all the letters were capped. When I changed that, the code ran as expected.

    Lesson learned. Many thanks for your time.

    chris
    Last edited by Repent34; 04-04-2011 at 06:36 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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