title: COM (MS Office) eventing in Powershell
body: <p>This question is strongly related to my prior one here: <a href="https://stackoverflow.com/questions/54146978/handling-com-event-cancelation-in-vbscript/54154111#54154111">Handling COM event cancelation in VBScript</a></p>
  
  <p>I would like to write a script, that sends an E-Mail via our companys SMTP-Server using CDO.</p>
  
  <p>First I tried to write an HTA-application for that purpose, but it became rather fiddly to make it cormfortable enough so that other people may handle it well (because of proper recipient resolving).</p>
  
  <p>So now I try to use the regular Outlook-Mail mask to prepare the mail first and then catch the send-item event via Powershell to give it's content to my CDO script. I tried it via VBScript before (see link above) but that doesn't seem to work as intended.</p>
  
  <p>I'm rather new to powershell-scripting but in the past I already got things to work that wouldn't work in VBScript so I hope to get my problem solved with it again. Right now, my script looks like this:</p>
  
  <pre><code>$WSH = new-Object -ComObject "WScript.Shell"
  $Outlook = new-Object -ComObject "Outlook.Application"
  $olMailItem = 0
  
  register-objectevent -InputObject $Outlook -EventName "ItemSend"
  
  $MailObject = $Outlook.CreateItem($olMailItem)
  $MailObject.Display()
  
  function Outlook_ItemSend([object] $Item, [ref] $Cancel)
  {
      $Cancel = $TRUE
      $WSH.PopUp("Event worked")
      return $Null
  }
  </code></pre>
  
  <p>I didn't come as far as I was hoping yet, because I can't figure out how to fire the event at all. You can find plenty of examples for <code>register-objectevent</code> online, but everytime it appears to be very very specific for some special COM object. I couldn't find any example to register an MS-Office COM event that requires parameters so far...</p>