Consulting

Results 1 to 11 of 11

Thread: delete workbook after a certin time period

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    delete workbook after a certin time period

    can this be done in code?
    Peace of mind is found in some of the strangest places.

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    This should do it![VBA]
    Private Sub Workbook_Open()
    If Date >= "26/09/08" Then
    MsgBox "Trial period has expired please call John Doe", vbOKOnly, "Trial Period Expired"
    Call KillActive
    End If
    End Sub

    Sub KillActive()
    Dim sName As String
    On Error Resume Next
    sName = ThisWorkbook.FullName
    Application.DisplayAlerts = False
    ThisWorkbook.ChangeFileAccess xlReadOnly
    Kill sName
    Application.DislayAlerts = True
    ThisWorkbook.Close SaveChanges:=False
    Application.Quit
    End Sub[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    thanks. that should do it
    Peace of mind is found in some of the strangest places.

  4. #4
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    this would only work if they enabled vba when they opened the workbook though, correct?

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Yes correct you would have to forc macro use too by hiding all sheets (xlveryhidden) except one and unhiding during the workbook open event.

    Add o worksheet call it Welcome and use this code (adapt to use the trial period code within it!)
    [VBA]Private Sub Workbook_Open()
    Dim Sht As Worksheet
    Application.ScreenUpdating = False
    For Each Sht In Sheets
    If Not Sht.Name = "Welcome" Then
    Sht.Visible = xlSheetVisible
    End If
    Next
    Sheets("Welcome").Visible = xlSheetVeryHidden
    Application.ScreenUpdating = True
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Sht As Worksheet
    Application.ScreenUpdating = False
    Sheets("Welcome").Visible = xlVisible
    For Each Sht In Sheets
    If Not Sht.Name = "Welcome" Then
    Sht.Visible = xlSheetVeryHidden
    End If
    Next
    Application.ScreenUpdating=True
    End Sub [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  6. #6
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    ha. I've never done that before using very hidden. slick.

  7. #7
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Where does that code go? I put it in a module and it doesn't appear to do anything when i close a workbook or open it.

  8. #8
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    I put it in the workbook and it almost works.

    When I close, I get Run-time error '1004': Unable to set the Visible property of the Worksheet class.

    Thoughts?

  9. #9
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Was just missing the sheet from x1SheetVisible.

    [VBA]Private Sub Workbook_Open()
    Dim Sht As Worksheet
    Application.ScreenUpdating = False
    For Each Sht In Sheets
    If Not Sht.Name = "Welcome" Then
    Sht.Visible = xlSheetVisible
    End If
    Next
    Sheets("Welcome").Visible = xlSheetVeryHidden
    Application.ScreenUpdating = True
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Sht As Worksheet
    Application.ScreenUpdating = False
    Sheets("Welcome").Visible = xlSheetVisible
    For Each Sht In Sheets
    If Not Sht.Name = "Welcome" Then
    Sht.Visible = xlSheetVeryHidden
    End If
    Next
    Application.ScreenUpdating=True
    End Sub [/VBA]

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @Simon:

    Hi Simon,

    Say, reference using the string...

    Quote Originally Posted by Simon Lloyd
    This should do it![vba]
    Private Sub Workbook_Open()
    If Date >= "26/09/08" Then
    MsgBox "Trial period has expired please call John Doe", vbOKOnly, "Trial Period Expired"
    Call KillActive
    End If
    End Sub
    [/vba]
    ...I tried a couple of tests. Here (U.S.), if I hover over Date while stepping thru, it reads "9/21/2010" due to m/d/yyyy settings. So I changed the string to ""09/26/08" and it passed.

    The trouble is that so did "09/26/10". Only if I put the exact same formatting to the string, "9/26/2010" does it recognize that the date is farther out.

    Anyways, my suggestion would be to use DateSerial, because AFAIK, this will bypass any formatting/local settings issues, as we are comparing a Date to a Date.
        If Date >= DateSerial(2010, 9, 26) Then
    A great day to all,

    Mark

  11. #11
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Mark, good catch, i didn't think of the code being used outside of the uk or uk date formatting, just no forward planning on my behalf
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

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