title: Syntax error with AddHandler in VBScript
body: <p>First off, I'm pretty newbie to the VB language, VB scripting and VB.net programming, but rather good at other languages and platforms.</p>
  
  <p>My goal is to run a simple VB-based program from the command line and have it popping a message box up (that part I figured it out). In addition, I want the message box (as well as the script) to terminate if the computer goes in sleep mode (or if it resumes from it) in the mean time.</p>
  
  <p>I found this code on the VBForums about the handler:</p>
  
  <pre><code>'add the handlers for the system standby, resume, and shutdown
  AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PowerModeChanged
  AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf SessionEnding
  
  [...]
  
  Private Sub PowerModeChanged(ByVal sender As System.Object, _
   ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
      Select Case e.Mode
          Case Microsoft.Win32.PowerModes.Resume
              'windows is resuming from sleep
          Case Microsoft.Win32.PowerModes.Suspend
              'goodnite windows
      End Select
  End Sub
  
  Private Sub SessionEnding(ByVal sender As System.Object, _
  ByVal e As Microsoft.Win32.SessionEndingEventArgs)
      Select Case e.Reason
          Case Microsoft.Win32.SessionEndReasons.Logoff
              'logoff
          Case Microsoft.Win32.SessionEndReasons.SystemShutdown
             'shutdown
      End Select
  End Sub
  </code></pre>
  
  <p>So I made a .vbs file that uses the above (first version) and ran it:</p>
  
  <pre><code>Sub PowerModeChanged(ByVal sender As System.Object, _
      ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
      Select Case e.Mode
          Case Microsoft.Win32.PowerModes.Resume
              'windows is resuming from sleep
              WScript.Quit
          Case Microsoft.Win32.PowerModes.Suspend
              'goodnite windows
              WScript.Quit
      End Select
  End Sub
  
  Sub Main()
      Set objArgs = WScript.Arguments
      msgText = objArgs(0)
      AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PowerModeChanged
      MsgBox msgText
  End Sub
  
  Main()
  </code></pre>
  
  <p>But I got all kinds of syntax errors (on <code>As</code>, <code>AddressOf</code>, etc.). After some tests and googling around I came to realize that some VB entities appears to be more type-strict and has a more evolved syntax than others. So here's my second version which almost succeeds to pass the syntax phase:</p>
  
  <pre><code>Sub PowerModeChanged(sender, e)
      Select Case e.Mode
          Case Microsoft.Win32.PowerModes.Resume
              'windows is resuming from sleep
              WScript.Quit
          Case Microsoft.Win32.PowerModes.Suspend
              'goodnite windows
              WScript.Quit
      End Select
  End Sub
  
  Sub Main()
      Set objArgs = WScript.Arguments
      msgText = objArgs(0)
      AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, PowerModeChanged
      MsgBox msgText
  End Sub
  
  Main()
  </code></pre>
  
  <p>Now the interpreter complains about "Require: 'Microsoft'" at the <code>AddHandler</code> line. At this point I don't know what to do. I tried <code>Microsoft.AddHandler</code> but did not work.</p>
  
  <p>So I would appreciate if you could help me fix this line, and tell me if there's any other thing that could make this little program working.</p>
  
  <p>Thank you.</p>