Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We're using Word 2010 and have a PowerShell script that reads a table in a Word docx file and use it to populate the autocorrect dictionary. It works for the regular entries that don't require formatting; but it breaks on the items that need to use the AddRichText method. It gives this error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x800A16DC): Reques
ted object is not available.
   --- End of inner exception stack trace ---
   at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParamet
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInf
o culture, String[] namedParams)
   at System.Management.Automation.ComMethod.InvokeMethod(PSMethod method, Object[] arguments)

And the code for the PowerShell is as follows; it reads from a 3 column table in the Word doc and uses them to populate the autocorrect. The first column has the Name, the next has the Value, and the last can be an X to indicate if it's rich text (has formatting).

$objWord = New-Object -Com Word.Application

$filename = 'D:\Codes.docx'
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

$word = New-Object -ComObject word.application
$word.visible = $true
$entries = $word.AutoCorrect.entries

Write-output "Starting to write... "

for($r=1; $r -le $LETableRows; $r++) {
    $replace = $LETable.Cell($r,1).Range.Text
    $replace = $replace.Substring(0,$replace.Length-2)
    $with = $LETable.Cell($r,2).Range.Text
    $withRange = $LETable.Cell($r,2).Range
    $with = $with.Substring(0,$with.Length-2)
    $format = $LETable.Cell($r,3).Range.Text
    $format = $format.Substring(0,$format.Length-2)

            if($format -eq "X") {
                $entries.addrichtext($replace, $withRange) | out-null 
            else {
                $entries.add($replace,$with) | out-null 
    Catch [system.exception]
          Write-Host $_.Exception.ToString()


 #$word = $null



# Stop Winword Process
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
share|improve this question
up vote 2 down vote accepted

The code looks correct and unfortunately the exception details are ambiguous and no help. The only thing I can think of that might be wrong is that AddRichText might not like being passed a cell range instead of paragraph range which is what it expects. Try changing that and see if it helps.

share|improve this answer
Thanks, Adam. How would I change the code to read a paragraph range? – Alex Dec 7 '12 at 6:08
Try something like: $withRange = $LETable.Cell($r,2).Range.Paragraphs(0).Range. However note it's just a stab in the dark :) – Adam Dec 7 '12 at 10:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.