title: Error 52 - bad file number
body: <p>the following VBA code is supposed to open a text file, read the number in it to a text file, add 1 to the number in the text file, then save the new number in the text file.</p>
  
  <p>I am currently getting a Error 52 at the line indicated (<code>While Not EOF(filenum)</code>). How do I fix this?</p>
  
  <pre><code>Public Sub Items_ItemAdd(Item As Outlook.MailItem)
  Dim filenum As Integer
  Dim current_number As String
  Dim fileName As String
  Dim objNS As Outlook.NameSpace
  Set objNS = GetNamespace("MAPI")
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
  filenum = FreeFile()
  fileName = "G:\Infrastructure Services\Engineering Services\Hazard Report Number.txt"
  Open fileName For Input As filenum
  While Not EOF(filenum) '*error 52 - Bad file number
  Line Input #filenum, current_number
  Close filenum
  Wend
  
  
  If Item.Class = olMail Then
      If Left$(Item.Subject, 29) = "Hazard Identification Report" Then
          Dim Msg As Outlook.MailItem
          Dim NewForward As Outlook.MailItem
          Dim myFolder As Outlook.MAPIFolder
          Dim olApp As Outlook.Application
          Dim olNS As Outlook.NameSpace
  
          Set Msg = Item
          Set NewForward = Msg.Forward
          Set olApp = Outlook.Application
          Set olNS = olApp.GetNamespace("MAPI")
  
          strSender = ""
  strsenderName = Msg.SenderEmailAddress
  
  
  
  
  
  If strsenderName = "EX" Then
    Set objSender = itm.Sender
    If Not (objSender Is Nothing) Then
      Set objExchUser = Sender.GetExchangeUser()
      If Not (objExchUser Is Nothing) Then
        strSender = objExchUser.PrimarySmtpAddress
      End If
    End If
  Else
    strSender = strsenderName
  End If
  
  
  
          With NewForward
              .Subject = "Hazard report reciept number: &amp; "
              .To = strSender
              .HTMLBody = "TYhank you for your email"
              .Send
          End With
          End If
      End If
      Close filenum
  
  ExitProc:
      Set NewForward = Nothing
      Set Msg = Nothing
      Set olApp = Nothing
      Set olNS = Nothing
  
  
  End Sub
  </code></pre>