title: Workbook opens again when I close it if opened by someone else also
body: <p>Sorry about the confusing title. But if a file is opened by someone else, when I open it I get the message that it's opened by someone else and I get the option to open it without being able to save it.<br />
  If I choose that option when I close the file it auto opens again.</p>
  <p>The file has a few macros that is most likely the cause but I can't understand how it causes this issue.</p>
  <p>First of all. The file has an inactivity tracker in a module that runs every five seconds:</p>
  <pre><code>Private Type LASTINPUTINFO
    cbSize As Long
    dwTime As Long
  End Type
  
  #If VBA7 Then
      Private Declare PtrSafe Function GetTickCount Lib &quot;kernel32&quot; () As Long
      Private Declare PtrSafe Sub GetLastInputInfo Lib &quot;User32&quot; (ByRef plii As LASTINPUTINFO)
  #Else
      Private Declare Function GetTickCount Lib &quot;kernel32&quot; () As Long
      Private Declare Sub GetLastInputInfo Lib &quot;User32&quot; (ByRef plii As LASTINPUTINFO)
  #End If
  
  Public tid As Variant
  Public lista As Scripting.Dictionary
  
  
  Function IdleTime() As Single
    Dim a As LASTINPUTINFO
    a.cbSize = LenB(a)
    GetLastInputInfo a
    IdleTime = (GetTickCount - a.dwTime) / 1000
  End Function
  
  Sub Form_Timer()
  
      
      ' lookup the inaktivity time for current user if 0
      If tid = 0 Then
          LR = ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Cells(ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Rows.Count, &quot;B&quot;).End(xlUp).Row
          tid = Application.VLookup(UCase(Environ(&quot;UserName&quot;)), ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Range(&quot;B17:G&quot; &amp; LR), 6, False)
          If Not IsError(tid) Then
              tid = tid * 60
          Else
              ' if user does not have a specified inactivity time set the &quot;other&quot; time to user
              tid = Application.VLookup(&quot;Övriga&quot;, ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Range(&quot;B17:G&quot; &amp; LR), 6, False)
              tid = tid * 60
          End If
      End If
      
      
      
      tme = IdleTime
      'Debug.Print tme &amp; &quot; &quot; &amp; Now()
      
      ' display warning when less than 65 seconds
      If tid - tme &lt; 65 Then
          UserForm2.Show vbModeless
          DoEvents
      End If
      
      If tme &gt;= tid Then
          If lista.Exists(UCase(Environ(&quot;UserName&quot;))) Then ThisWorkbook.Save
          Application.DisplayAlerts = False
          ThisWorkbook.Close
  
      End If
      On Error Resume Next
      Application.OnTime RunTime, &quot;Form_Timer&quot;, Schedule:=False
      Application.OnTime Now + TimeSerial(0, 0, 5), &quot;Form_Timer&quot;
  End Sub
  </code></pre>
  <p>This code is initiated with the workbook_open event below.
  The dictionary created is to save the inactivity times (and write permissions) in a dictionary so that it &quot;can't&quot; be manipulated without save permission.</p>
  <pre><code>Public RunTime
  
  Private Sub Workbook_Open()
      Set lista = New Scripting.Dictionary
      For I = 18 To ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Range(&quot;B200&quot;).End(xlUp).Row
          If ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Range(&quot;B&quot; &amp; I).Value &lt;&gt; &quot;&quot; And ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Range(&quot;B&quot; &amp; I).Value &lt;&gt; &quot;Övriga&quot; Then
              lista.Add Key:=ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Range(&quot;B&quot; &amp; I).Value, Item:=ThisWorkbook.Sheets(&quot;Inaktivitet&quot;).Range(&quot;C&quot; &amp; I).Value
          End If
      Next I
      
      Application.Calculation = xlCalculationManual  ' This is done to make the workbook more responsive due to other event macros
      Form_Timer   ' &lt;----  Here
  End Sub
  </code></pre>
  <p>And in BeforeSave I make sure the username is in the dictionary, if not they are not allowed to save, and in BeforeClose I turn off the inactivity tracker and set calculation to automatic:</p>
  <pre><code>Private Sub Workbook_BeforeClose(Cancel As Boolean)
      On Error Resume Next
      Application.OnTime RunTime, &quot;Form_Timer&quot;, Schedule:=False
      Application.Calculation = xlAutomatic
  End Sub
  
  Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      On Error GoTo err
      If Not lista.Exists(UCase(Environ(&quot;UserName&quot;))) Then
          Cancel = True
          MsgBox &quot;Du har inte behörighet att spara schemat&quot; ' you don't have permissions to save.
           
      End If
      GoTo subend:
      
      
  err:
      MsgBox &quot;Något har gått fel, det går inte spara.&quot; &amp; vbNewLine &amp; &quot;filen går in i felsäkert läge nu. Kopiera celler/blad som är ändrade till en ny excelfil och spara den.&quot; &amp; vbNewLine &amp; &quot;Stäng därefter alla Excelfiler innan du försöker öppna någon Excelfil igen.&quot;
      Application.EnableEvents = False
      
  subend:
      On Error GoTo 0
  End Sub
  </code></pre>
  <p>When the user change active workbook the calculation toggles:
  But I doubt this is the cause of it.</p>
  <pre><code>Private Sub Workbook_WindowActivate(ByVal Wn As Window)
      If Wn.Caption = ThisWorkbook.Name Then
          Application.Calculation = xlCalculationManual
      Else
          Application.Calculation = xlCalculationAutomatic
      End If
  End Sub
  
  Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
      Application.Calculation = xlCalculationAutomatic
  End Sub
  </code></pre>
  <p>The rest of the macro running is SheetChange, SheetActivate, and SheetSelectionChange code to color text on the sheets to help the user and display various messages.</p>
  <p>What I believe is the issue is that the inactivity timer, that it by some reason runs again after the workbook has been closed and thus open the workbook to do it.<br />
  Or do you see something else that cause the workbook to auto open again? It only happens when someone else has the write permissions and I open it no write permissions.<br />
  When I close the workbook I get the question if I want to save the changes and I press No.<br />
  This should turn off the inactivity timer and then close it, and stop. At least as I see it.</p>
  <p>I know that the inactivity timer can be turned off by a skilled person and that the write permission using the dictionary is not 100% safe, but it's not intended to be.</p>