title: How to debug a powershell exception calling "InvokeMember" with "5" argument(s): Type mismatch
body: <p>I'm trying to implement this code example from the Scripting Guy: <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/06/hey-scripting-guy-how-can-i-add-custom-properties-to-a-microsoft-word-document.aspx" rel="nofollow">http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/06/hey-scripting-guy-how-can-i-add-custom-properties-to-a-microsoft-word-document.aspx</a></p>
  
  <p>Code: </p>
  
  <pre><code>$path = "C:\fso\Test.docx"
  $application = New-Object -ComObject word.application
  $application.Visible = $false
  $document = $application.documents.open($path)
  $binding = "System.Reflection.BindingFlags" -as [type]
  
  $customProperties = $document.CustomDocumentProperties
  $typeCustomProperties = $customProperties.GetType()
  
  $CustomProperty = "Client"
  $Value = "My_WayCool_Client"
  [array]$arrayArgs = $CustomProperty,$false, 4, $Value
  
  Try
   {
    $typeCustomProperties.InvokeMember(`
      "add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
      out-null
   }
  Catch [system.exception]
   {
    $propertyObject = $typeCustomProperties.InvokeMember(`
      "Item", $binding::GetProperty,$null,$customProperties,$CustomProperty)
    $typeCustomProperties.InvokeMember(`
      "Delete", $binding::InvokeMethod,$null,$propertyObject,$null)
    $typeCustomProperties.InvokeMember(`
      "add", $binding::InvokeMethod,$null,$customProperties,$arrayArgs) |
      Out-Null
   }
  
  $document.Saved = $false
  $document.save()
  $application.quit()
  $application = $null
  [gc]::collect()
  [gc]::WaitForPendingFinalizers()
  </code></pre>
  
  <p>Error (I removed the Try-Catch wrapping, which is intended to handle the situation when the custom property being added already exists in the docx file, which it doesn't): </p>
  
  <pre><code>Exception calling "InvokeMember" with "5" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
  At C:\Set-WordCustomProperties.ps1:16 char:3
  </code></pre>
  
  <p>All the different InvokeMethod lines end in some similar error. The error message is not very helpful to debug. Is there any way to get more info on what is incorrect?</p>