0

I'm using a PowerShell script to batch convert Word document to Pdfs.

The conversion part of the script is (if required I can paste the whole script):

$word = New-Object -ComObject "word.application"

$outputFile = $outputDirectory + "\" + "myPdf.pdf" 

$doc = $word.documents.Open($inputFile, $refFalse,  $true) # open in background - No UI
$doc.SaveAs([ref]$outputFile, [ref]17) #17 is for PDF
$doc.Saved = $true
write-host "Processed $outputFile" -foregroundcolor Green 
$doc.Close()
$word.Quit()

The script is working quite well, but some of the source documents are corrupted. When Word detects one of these documents, it displays the repair dialog. This causes my script to be blocked until the user close the dialog.

How can I prevent this dialog ?

[Edit] here is a screenshot of the dialog

Show Repairs dialog

Kara
  • 6,115
  • 16
  • 50
  • 57
Steve B
  • 36,818
  • 21
  • 101
  • 174

5 Answers5

1

I think Christian's may work if you set the DisplayAlerts parameter to $false.

Try the below, replacing the path:

$filePath = "path\to\excel.xslx" # replace filename

# function pulled from http://stackoverflow.com/questions/5544844/how-to-call-a-complex-com-method-from-powershell
# allows for calling complex COM object's methods... I can define which arguments I want to send in
Function Invoke-NamedParameter {
    [CmdletBinding(DefaultParameterSetName = "Named")]
    param(
        [Parameter(ParameterSetName = "Named", Position = 0, Mandatory = $true)]
        [Parameter(ParameterSetName = "Positional", Position = 0, Mandatory = $true)]
        [ValidateNotNull()]
        [System.Object]$Object
        ,
        [Parameter(ParameterSetName = "Named", Position = 1, Mandatory = $true)]
        [Parameter(ParameterSetName = "Positional", Position = 1, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$Method
        ,
        [Parameter(ParameterSetName = "Named", Position = 2, Mandatory = $true)]
        [ValidateNotNull()]
        [Hashtable]$Parameter
        ,
        [Parameter(ParameterSetName = "Positional")]
        [Object[]]$Argument
    )

    end {  ## Just being explicit that this does not support pipelines
        if ($PSCmdlet.ParameterSetName -eq "Named") {
            ## Invoke method with parameter names
            ## Note: It is ok to use a hashtable here because the keys (parameter names) and values (args)
            ## will be output in the same order.  We don't need to worry about the order so long as
            ## all parameters have names
            $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                $null,  ## Binder
                $Object,  ## Target
                ([Object[]]($Parameter.Values)),  ## Args
                $null,  ## Modifiers
                $null,  ## Culture
                ([String[]]($Parameter.Keys))  ## NamedParameters
            )
        } else {
            ## Invoke method without parameter names
            $Object.GetType().InvokeMember($Method, [System.Reflection.BindingFlags]::InvokeMethod,
                $null,  ## Binder
                $Object,  ## Target
                $Argument,  ## Args
                $null,  ## Modifiers
                $null,  ## Culture
                $null  ## NamedParameters
            )
        }
    }
}

# create Excel COM object, set to suppress alert boxes
$excelapp = new-object -com Excel.Application
$excelapp.displayalerts = $false

# open workbook with CorruptLoad = Repair
[void](invoke-namedparameter $excelapp.workbooks "Open" @{"Filename"=$filepath; "CorruptLoad"=2})

# save repaired file and close
$excelapp.activeworkbook.saveas($filepath)
$excelapp.quit()
Daniel Richnak
  • 1,584
  • 8
  • 12
  • there is no DisplayAlerts parameter (http://msdn.microsoft.com/fr-fr/library/microsoft.office.interop.word.documents.open.aspx) – Steve B Dec 12 '11 at 08:45
  • It's not a parameter of the Open method, it's a property of the Excel.Application itself: http://msdn.microsoft.com/en-us/library/bb177478%28v=office.12%29.aspx – Daniel Richnak Dec 12 '11 at 18:35
  • There is no DisplayAlerts for word (http://msdn.microsoft.com/en-us/library/ff198329.aspx). Still, when you read the documentation, it states "True if Microsoft Excel displays certain alerts and messages while a **macro** is running.". I don't think this applies in my case. Anyways, I vote up for the tips about how to call complex parameters. – Steve B Dec 13 '11 at 08:29
  • I wasn't reading close enough earlier to realize you were working in Word and not Excel (as I had previous encounter w/Excel). However.. from the link you provide, Word does have this property :) http://msdn.microsoft.com/en-us/library/ff192373.aspx Either way, sounds like using OpenNoRepairDialog works for your needs. I wonder, does that actually repair the file, or just suppress the alert? When you save the file it probably fixes it... so both methods are most likely functionally equivalent. – Daniel Richnak Dec 13 '11 at 20:55
  • @DanielRichnak - I have an answer and a question. First, yes, it does still try to repair. I know this because it's what spawned my next question... I am opening files as `ReadOnly:true` so when it repairs behind the scenes it tries to save and thus another dialog box opens... how can I simply ensure saves are never attempted? – one.beat.consumer Jun 28 '12 at 19:18
1

I was simply looking at the wrong direction.

There is no parameter of the Open method allowing to disable this dialog, but I found there is another method : OpenNoRepairDialog.

Simple... just have to look around a bit

Steve B
  • 36,818
  • 21
  • 101
  • 174
0

I know this is old now, but just in case it helps someone else, you could try adding:

$word.visible = false

$word.DisplayAlerts = "wdAlertsNone"

Community
  • 1
  • 1
Mark
  • 1,455
  • 3
  • 28
  • 51
0

You can try open your documents like this:

$word.documents.Open($inputFile, $refFalse,  $true, $null, $null, $null, $null, $null, $null, $null, $null, $null, $false, $null, $null, $null)

In this way you are telling Word to not repair corrupted file.... but I can't test it and I don't know the behaviour in you script.

Take a look here for other param MSDN

CB.
  • 58,865
  • 9
  • 159
  • 159
0

I met the same problem, my script don't close correctly the document try this to avoid damaging your document :

# Create Word Object  
$wrd = new-object -com "word.application"

# Make Word Visible  
$wrd.visible = $true

# Open a document   
$doc = $wrd.documents.open("C:\silogix\silogix.doc") 

# Your work
# ...

# Stop Winword Process in a good way
$wrd.quit()
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wrd)
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • I can't control the source. I get a bunch of documents from an external system, and I have to convert it to pdf... The ReleaseComObject is a good point in general, but I don't know how it can help me – Steve B Dec 09 '11 at 16:44