+ Reply to Thread
Results 1 to 3 of 3

Need Help Using Windows Timers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-25-2010
    Location
    Phoenix, Arizona, USA
    MS-Off Ver
    O365
    Posts
    202

    Need Help Using Windows Timers

    Greetings, I have a WB that is used among my department. If one user 'forgets' to close the WB, it locks everyone else out. I've tried using a SHARED WB, but that has lead to other issues, and from what read, it's just as well that I don't use a SHARED WB.

    To my point...I was given reference to http://www.cpearson.com/Excel/OnTime.aspx and have attempted to use the provided example. When I use the line of
    ThisWorkbook.Close SaveChanges:=False
    the entire Excel application gets closed. I only want to close the WB, in case the user has other WB's open, I don't need to disturb them.

    Here's the code I have, no other code is in the WB.
    Public Declare Function SetTimer Lib "user32" ( _
        ByVal HWnd As Long, _
        ByVal nIDEvent As Long, _
        ByVal uElapse As Long, _
        ByVal lpTimerFunc As Long) As Long
    
    Public Declare Function KillTimer Lib "user32" ( _
        ByVal HWnd As Long, _
        ByVal nIDEvent As Long) As Long
    
    Public TimerID As Long
    Public TimerSeconds As Single
    
    Public Sub StartTimer()
    
        TimerSeconds = 10 ' how often to "pop" the timer.
        TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
        
    End Sub
    
    Public Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal dwTimer As Long)
        
        KillTimer 0&, TimerID
        
        ChDrive "C"
        ChDir "C:\Temp"
        
        With ThisWorkbook
            'Save a TEMP file - just incase.
            .SaveAs Filename:=Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5) & " " & Format(Now(), "mmddyy hhmm"), _
                    FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
                    AccessMode:=xlExclusive, _
                    AddToMRU:=True
            .Close SaveChanges:=False   '<<<<----  THIS IS THE OFFENDING LINE
        End With
        
    End Sub
    You help is appreciated...

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Need Help Using Windows Timers

    I have Excel 2003 and can use ThisWorkbook.Close to close only the workbook and not the application.

    As a test, can you run this macro with a new workbook.
    Sub Macro1()
        ThisWorkbook.Close SaveChanges:=False
    End Sub

  3. #3
    Forum Contributor
    Join Date
    10-25-2010
    Location
    Phoenix, Arizona, USA
    MS-Off Ver
    O365
    Posts
    202

    Re: Need Help Using Windows Timers

    Yes, that line of code does work, but when you put it together with the rest of the code, the TimerProc is killing my Excel application too.

+ 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