title: Troubles using Powershell and Word SaveAs()
body: <p>From examples on the net, I have cobbled together this script which I hope will save Word <em>.doc</em> files as plainText. It does not.</p>
  
  <p>First, the Word window appears when I have given $word.Visible = $False.</p>
  
  <p>Then, a dialog appears asking if I want to open a read-only copy. No one else has the document open. Why is it asking.</p>
  
  <p>Finally, the error message says I have a type mismatch. Why is that?</p>
  
  <pre><code>PS H:\d2&gt; .\wl.ps1
  Processing : H:\d2\checklists\Extract checklist_20120306.doc
  Name is now: H:\d2\checklists\Extract checklist_20120306.txt
  Exception calling "SaveAs" with "2" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
  At H:\d2\wl.ps1:19 char:5
  +     $opendoc.SaveAs([ref]$name.Value,[ref]$saveFormat)
  +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : COMException
  </code></pre>
  
  <p>Here is the source code for the script.</p>
  
  <pre><code>$srcfiles = Get-ChildItem -Path . -Recurse -Filter "*.doc*"
  $saveFormat = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatText
  $word = new-object -comobject word.application
  $word.Visible = $False
  
  ForEach ($doc in $srcfiles) {
      Write-Host "Processing :" $doc.fullname
      $name = Join-Path -Path $doc.DirectoryName -ChildPath $($doc.BaseName + ".txt")
      Write-Host "Name is now:" $name
  
      $opendoc = $word.documents.open($doc.FullName)
      $opendoc.SaveAs([ref]$name.Value,[ref]$saveFormat)
      $opendoc.Close()
  
      $doc = $null
  }
  
  $word.quit()
  </code></pre>