title: VBA: Sub to Write to a Log File
body: <p>I have a set of macros defined in my workbook, and I'd like to offer the user the option to log events related to those macros in a log file.</p>
  
  <p>I initiate the log by creating the following in ThisWorkbook:</p>
  
  <pre><code>Public writeLog as Boolean
  Public logWrite as Object
  Public log as Object
  Private Sub Worksheet_Open()
      Dim prompt as Integer
      prompt = MsgBox("Would you like to log events for this session?", vbYesNo, "Log Events?")
      If prompt Then
          writeLog = True
          Set logWrite = CreateObject("Scripting.FileSystemObject")
          Set log = logWrite.CreateTextFile("C:/TEST.txt", False)
      Else
          writeLog = False
      End If
  End Sub
  </code></pre>
  
  <p>I then created a procedure that I can use to write an argument to this object, which I've stored in its own module:</p>
  
  <pre><code>Public Sub PrintLog(obj as Object, argument as String)
      If writeLog = True Then
          obj.WriteLine argument
      End If
  End Sub
  </code></pre>
  
  <p>Unfortunately, this doesn't work, and I'm not sure why: even if I don't include <code>obj</code> as an argument to the function (since <code>log</code> and <code>logWrite</code> were created as global variables), I'm not able to <code>Call WriteLog("String here.")</code> or <code>Call WriteLog(log, "String here.")</code> without an error (<code>Compile Error: Argument Not Optional.</code>) </p>
  
  <p>Is it possible to get such a <code>Sub()</code> to work, so that I can call it from anywhere in the workbook (after a button is pressed in a userform, for example) without having to define a new <code>Scripting.FileSystemObject</code> in every module?</p>