title: Permission Denied error in excel vba
body: <p>I am writing a function in excel vba </p>
  
  <pre><code> Function WriteByteArray(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = False) As Boolean
      Dim iFileNum As Integer, lWritePos As Long
  
      Debug.Print " --&gt; Entering WriteByteArray function with " &amp; sFileName &amp; " file to write."
      On Error GoTo ErrFailed
      If bAppendToFile = False Then
          If Len(Dir$(sFileName)) &gt; 0 And Len(sFileName) &gt; 0 Then
              'Delete the existing file
              VBA.Kill sFileName
          End If
      End If
  
      iFileNum = FreeFile
      Debug.Print "iFileNum = " &amp; iFileNum
      'Open sFileName For Binary Access Write As #iFileNum
      Open sFileName For Binary Lock Read Write As #iFileNum
  
      If bAppendToFile = False Then
          'Write to first byte
          lWritePos = 1
      Else
          'Write to last byte + 1
          lWritePos = LOF(iFileNum) + 1
      End If
  
      Dim buffer() As Byte
      buffer = vData
      Put #iFileNum, lWritePos, buffer
  
      WriteByteArray = True
      Exit Function
  ErrFailed:
          Debug.Print "################################"
          Debug.Print "Error handling of WriteByteArray"
          Debug.Print "################################"
          FileWriteBinary = False
          Close iFileNum
          Debug.Print Err.Description &amp; "(" &amp; Err.Number &amp; ")"
      End Function
  </code></pre>
  
  <p>I am getting a permission denied error in VBA.Kill and Open Can any one help me???</p>