title: "File already open" when trying to open a text file
body: <p><strong>What I want to do:</strong> I have an <code>Log.txt</code> where special values from an Excel-Sheet are listed. Anytime the Macro is executed, it checks for new special values. Before adding them to the <code>Log.txt</code> via a Sub, the same Sub checks if the corresponding value (they are unambigous) is already on the Log-List. If this is not the case, the value should be added to the list.</p>
  
  <p><strong>My approach:</strong> You can see my current approach in the code example below.</p>
  
  <pre><code>
  Dim FileNum as Integer
  dim DataLine as String
  Dim strPath as String
  Dim strEntry as String
  
  strPath = [Path to Log.txt]  
  strEntry = [Special Value]     
  
  'In this first part the Log.txt is opened for Input and each line is saved in DataLine 
  'to be compared to the special value in strEntry. If it is already in the Log.txt, the Sub to
  'create a new Log-Entry is exited and is started again, once the next special value from another cell is 
  'obtained (from another Sub).
  
  FileNum = FreeFile()
  Open strPath For Input As #FileNum
  Do While Not EOF(FileNum)
       Line Input #FileNum, DataLine
  'The value strEntry should start at position 2 of the Entry in the Log.txt (due to the quotation marks [""] in the 
  'Log.txt line.
       If InStr(DataLine, strEntry) = 2 Then Exit Sub
       Loop
  Close #FileNum
  
  'After it could be verified, that strEntry is not already in the Log.txt, the txt-File should be opened 
  'again, this time for Append. Then, the strEntry should be written to the txt-File, the Log.txt should close and 
  'Sub is finished.
  
  FileNum = FreeFile()
  Open strPath For Append As #FileNum
  Write #FileNum, strEntry 
  Close #FileNum
  </code></pre>
  
  <p><strong>The Problem:</strong> I observed, that the first part of the Sub is working fine. If <code>strEntry</code> is already in the <code>Log.txt</code>, the Sub is exited and the whole Macro is jumping to the next special value. If this value is not already in the txt-File, the first part of the Sub does not exit the Sub and it jumps to the second part, where it should append the value to the <code>Log.txt</code>. </p>
  
  <p>This is where the problem is. If I exclude the first part of the Sub, I could verify, that the second part is also working fine (as he simply appends all values to the txt-File). But once I have the first part included, I get the Error-Message </p>
  
  <blockquote>
    <p>File already open.</p>
  </blockquote>
  
  <p>I can not figure out, why this is happening, as I <code>Close #FileNum</code> at the end of part one. </p>
  
  <p>I thank in advance for your ideas and solutions.</p>