none
Error in opening & repairing word through Powershell script RRS feed

  • Question

  •    	    	Log-Write -LogPath "$Logpathfilename" -LineValue "ERROR: File already exists, skipping File Rename:    $NewDoc"
        	    	#Open Office file - NOTE YOU MUST MANUALLY EDIT THE FOLLOWING PATH!!!!!!!
        	    	#C:\"Program Files (x86)\Microsoft Office"\Office15\WinWord.exe $Doc
                    $word = New-Object -ComObject word.application
                    #$word.Visible = $true
                    
                    #$word.documents.Open($Doc, $false, $false, $false, $false, $false, $false, $false, $false, $false, $false, $true, $true)
                   $word.Documents.Open($Doc, $False,  $False, $False, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null, $null)
                  
        	    	#Log opening the file
    			    Log-Write -LogPath "$Logpathfilename" -LineValue "$DocLeaf	- Opening File"
                    RepairProgress "Repair Progress $count of $totcnt " "Input box contains current file path and name, select and paste into Save As Dialog box if neccessary. Select the checkbox on repair outcome."  "$Doc"  
                    # "Repair Outcome: $global:Outcome "
                    Log-Write -LogPath "$Logpathfilename" -LineValue "$DocLeaf	- Repair Outcome:   $global:Outcome"

    I am trying to use powershell script to open and repair the corrupted document. We nned to create an utility to process/repaira  bunch of corrupted documents. I am using the above open command with paramenters to open and repair the document. But this is giving me error as below.

    Exception calling "Open" with "16" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
    At \\AD\MyND_GL\Profile\jgeo1.AD\Desktop\Training\DO1.ps1:551 char:16
    +                $word.documents.Open($Doc, $False,  $False, $False, $null, $null, ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ComMethodTargetInvocation

    Monday, June 13, 2016 2:27 PM

Answers

  • >>>So I have tried with documents.open with parameters of repair as true. But throwing error.

    According to your description, you could make sure that parameters of Open method is correct, you could modify like below:
    $word.Documents.Open($Doc, $False,  $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

    or

    $word.Documents.Open($Doc, $False,  $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true)

    For more information, click here to refer about Documents.Open Method (Word)


    Wednesday, June 15, 2016 8:47 AM

All replies

  • Replace with $word.Documents.Open($Doc)

    Best regards, George

    Monday, June 13, 2016 2:55 PM
  • Hi Jomson George,

    According to your description, you could follow George.B.Summers's suggestion to open document.

    In addition you use below code to make sure this issue can be reproduced:

    $Filename='your word document file path'
    
    $Word=NEW-Object –comobject Word.Application
    
    $Document=$Word.documents.open($Filename)


    Tuesday, June 14, 2016 8:40 AM
  • Thank You George! This command works. It is opening the document. But my documents are corrupted and I want to repair the documents with the script. Is any other command or parameter to repair the document through powershell script. Once again, Thanks for your help!
    Tuesday, June 14, 2016 10:20 AM
  • Hello David, With this command i could open the document. But my documents are corrupted and I want to repair the documents with the script. So I have tried with documents.open with parameters of repair as true. But throwing error. It will be a great help if i could repair thru scripting. Please suggest. Thanks in advance!

    Tuesday, June 14, 2016 10:23 AM
  • Word should display a Repair Dialog automatically if the document you open is in error.

    Best regards, George

    Tuesday, June 14, 2016 10:39 AM
  • In addition you can specify OpenAndRepair in the Open function:

    $Doc = "E:\Dok\corrupt1.doc" $word = New-Object -ComObject word.application # # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $word.documents.Open($Doc, $false, $false, $false, "", "", $true, "", "", 0, $null, $true, $true, 0, $false) #, $null)

    # ... your code ...

    $word.ActiveDocument.Close(); $word.Quit() <# Open Parameters: Document Open( 1 string FileName, 2 bool ConfirmConversions, 3 bool ReadOnly, 4 bool AddToRecentFiles, 5 string PasswordDocument, 6 string PasswordTemplate, 7 bool Revert, 8 string WritePasswordDocument, 9 string WritePasswordTemplate, 10 int Format, 11 Object Encoding, 12 bool Visible, 13 bool OpenAndRepair, <- this 14 int DocumentDirection, 15 bool NoEncodingDialog, 16 Object XMLTransform ) #>



    Best regards, George

    Tuesday, June 14, 2016 12:17 PM
  • >>>So I have tried with documents.open with parameters of repair as true. But throwing error.

    According to your description, you could make sure that parameters of Open method is correct, you could modify like below:
    $word.Documents.Open($Doc, $False,  $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true, $null, $null)

    or

    $word.Documents.Open($Doc, $False,  $False, $False, $null, $null, $null, $null, $null, $null, $null, $null, $true, $true)

    For more information, click here to refer about Documents.Open Method (Word)


    Wednesday, June 15, 2016 8:47 AM
  • With all respect David, but did you do a test run with these parameters?


    Best regards, George

    Wednesday, June 15, 2016 2:04 PM
  • >>>but did you do a test run with these parameters?

    Sorry, I am not familiar with Powershell script, so I have created a sample with VBA, I find that OP's Open method have mistook parameter position through my comparasion.
    Friday, June 17, 2016 9:39 AM