Results 1 to 7 of 7

Thread: [RESOLVED] vba code to block an Excel file to be opened

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Resolved [RESOLVED] vba code to block an Excel file to be opened

    Hello to all of the forum,

    I am wirting this post as I do not know how to define a vba code in Excel.
    I am using Excel2003.

    I have a file which is limited of using.
    After 10 times using it, a Msgbox is opened, informing that the file will close and cannto be used anymore.

    I added following code in ThisWorbook:

    Option Explicit
    ' Limiter le nombre d'utilisation d'un classeur

    Private Sub Workbook_Open()
    On Error Resume Next
    If ActiveWindow.Visible = False Then ' Active Sheet not visible
    Application.Quit ' close Excel
    Exit Sub
    On Error Resume Next
    ElseIf Worksheets("Feuil2").Range("A6").Value >= 2 Then
    ActiveWindow.Visible = False
    Application.Quit
    Exit Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next
    With Worksheets("Feuil2").Range("A2").Value = Worksheets("Feuil2").Range("A2").Value + 1
    End With
    ActiveWorkbook.Save
    Application.Quit
    End If
    End Sub


    So far the vba code is functionning.

    As the files was opened 10 times, it is closing.
    When clicking o nthe file in the registry it is opening and closing immediatelay.

    I would like to ask how to define, which vba code is needed,
    that when the file is closing, that the file cannot be used anymore, cannot be opened at all. Means if clicking on it, gets shown a sing 'error'. The file is not opening.

    Thanks in advance for some information,

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: vba code to block an Excel file to be opened

    you can try like this
    vb Code:
    1. Private Sub Workbook_BeforeClose(Cancel As Boolean)
    2. Dim cnt As Integer
    3. cnt = ThisWorkbook.CustomDocumentProperties("count")
    4. If Not Err.Number = 0 Then
    5.      ThisWorkbook.CustomDocumentProperties.Add "count", False, msoPropertyTypeNumber, 0
    6. End If
    7. On Error GoTo 0
    8. ThisWorkbook.CustomDocumentProperties("count") = cnt + 1
    9. End Sub
    10.  
    11. Private Sub Workbook_Open()
    12. On Error Resume Next
    13. If ThisWorkbook.CustomDocumentProperties("count") > 9 Then ThisWorkbook.Close False
    14. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Re: vba code to block an Excel file to be opened

    Hello to all of the forum,

    thanks westconn1 for response,
    in your proposed code I get a bug in the procedure in : cnt = ThisWorkbook.CustomDocumentProperties("count")
    with following Msbox : ("argument or procedure is incorrect)

    The vba code which I used to open and close the file, runs well (see attached Excle file).

    But I would I like to ask, how to define the vbacode for following procedure :
    I have an Excel file that a user can open 10 times.
    When the user is opening more then 10 times, then is shown a Msgbox ("time of using is over") and the file is closing.
    This procedure is so far runing well.

    But which vba code to put,
    that when the file is closing, that afterwards this Excel file cannot be opened at all, means
    when clicking on the file in the directory to open, that is shown a Msgbox ("file invalid") and that the file does not opened at all.

    Thanks in advance for some response,

    news

    '
    Attached Files Attached Files
    Last edited by news; Jun 14th, 2010 at 03:59 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: vba code to block an Excel file to be opened

    in your proposed code I get a bug in the procedure in : cnt = ThisWorkbook.CustomDocumentProperties("count")
    i lost a line of code somewhere, i had it in originally, put
    on error resume next
    before that line

    AFAIK you can not prevent the workbook from opening, if clicked in the shell (explorer window), all you can do is close it immediately with or without messagebox, as below
    vb Code:
    1. Private Sub Workbook_Open()
    2. On Error Resume Next
    3. Select Case ThisWorkbook.CustomDocumentProperties("count")
    4.   case 10: msgbox "timeof using is over" : ThisWorkbook.Close False
    5.   case 11 to 14: msgbox " invalid file": ThisWorkbook.Close False
    6.   case > 14:ThisWorkbook.Close False
    7. end select
    8.  
    9. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Re: vba code to block an Excel file to be opened

    Re:

    thanks for vbacode, it is another possiblity as the one I used in the Excel file 'fermer_fichier' which I added before.

    I posted, as we got once a Excel file, to test.
    We tested with the examples proposed.

    To see which vbacode was used, we did Alt+F11, when clciking on the file,
    it was not possible as is was protected by password. A user entered twice some code, then the Excel file crashed,
    and afterwards it was not possible even to open. As soon was clicked in the directory in Windos Explorer, it was shown the Mscgbox, file invalid, corrupted, and it was not possible otp open at all.
    We could only delete the file.

    Somehow in the vbacode was put some code, which made the file corrupt,
    once Excel crashs down, or to find out vbaprotected password.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: vba code to block an Excel file to be opened

    for whatever reason, deliberately corrupting a file would be considered malicious and not a topic for here,
    best suggestion i have would be to delete the file, when finished with, or password protect
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    5

    Re: vba code to block an Excel file to be opened

    Re:

    as there is no vbcode to blok a file to open in windows explorer,
    I will use then the version of file is closing directly,
    as your vb code proposed,
    or as the version I use before in the Excel file 'fermer fichier',

    news

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width