How to debug a powershell exception calling "InvokeMember" with "5" argument(s): Type mismatch

-1

I'm trying to implement this code example from the Scripting Guy: 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

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()

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):

Exception calling "InvokeMember" with "5" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
At C:\Set-WordCustomProperties.ps1:16 char:3

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?

powershell
asked on Stack Overflow Oct 20, 2015 by gakera

1 Answer

1

Is there any way to get more info on what is incorrect?

There’s a couple of things you might try to get more detailed info. First, in your catch block, immediately capture $error[0] and $_. These two typically contain the most recent error info depending on what you were doing. Capturing them immediately will insure they are not overlaid by something in the catch block.

Catch [system.exception]
 {
  $err1 = $error[0]
  $err2 = $_
  $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
 }

Alternatively, you can use...

$error[0]|format-list –force

To force a more detail explanation. A Powershell Tips & Tricks post shows what the output would look like from the above command.

answered on Stack Overflow Oct 20, 2015 by rrirower

User contributions licensed under CC BY-SA 3.0