title: Copy used range to text file
body: <p>I want to:</p>
  
  <ul>
  <li>Copy the used range of a sheet called "Kommentar"</li>
  <li>Create a ".txt" file ("Kommentar.txt") in the same directory as ThisWorkbook</li>
  <li>Paste the previously copied used range</li>
  <li>Save the ".txt" file</li>
  </ul>
  
  <p>I have:</p>
  
  <pre><code>Sub CreateAfile()
  
  Dim pth As String
  pth = ThisWorkbook.path
  Dim fs As Object
  Set fs = CreateObject("Scripting.FileSystemObject")
  Dim a As Object
  Set a = fs.CreateTextFile(pth &amp; "\Kommentar.txt", True)
  Dim sh As Worksheet
  Set sh = ThisWorkbook.Sheets("Kommentar")
  
  Dim rng As Range
  Set rng = sh.UsedRange
  a.WriteLine (rng)
  a.Close
  
  End Sub
  </code></pre>
  
  <p>I get</p>
  
  <blockquote>
    <p>run-time error '13' Mismatch</p>
  </blockquote>
  
  <p>In line <code>a.WriteLine (rng)</code> the function doesn't accept range to be written.</p>