2011-12-09 48 views
0

我正在使用PowerShell腳本將Word文檔批量轉換爲Pdf。Powershell腳本與Word互操作,如何防止修復對話框打開?

腳本的轉換部分(如果需要的話我可以粘貼整個腳本):

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

劇本是合作得非常好,但一些源文件被損壞。 當Word檢測到其中一個文檔時,它會顯示修復對話框。這會導致我的腳本被阻塞,直到用戶關閉對話框。

如何防止此對話框?

[編輯]這裏的對話框

Show Repairs dialog

回答

1

我只是在尋找錯誤的方向。

沒有參數Open方法允許禁用此對話框,但我發現有另一種方法:OpenNoRepairDialog

簡單......只要看看周圍位

0

您可以嘗試打開您的文檔這樣的截圖:

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

這樣,你是在告訴Word將不修復損壞的文件....但我無法測試它,我不知道你腳本中的行爲。

這裏看看其他PARAM MSDN

+0

這是不夠的。對話框仍然在這裏 –

0

我遇到了同樣的問題,我的腳本不關閉正確的文件試試這個,以避免損壞您的文檔:

# 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) 
+0

我無法控制源代碼。我從外部系統獲得一堆文件,我必須將其轉換爲pdf ... ReleaseComObject是一個很好的總體點,但我不知道它如何能幫助我 –

1

我認爲基督教的五月如果將DisplayAlerts參數設置爲$ false,則工作。

嘗試以下,替換路徑:

$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() 
+0

沒有DisplayAlerts參數(http ://msdn.microsoft.com/fr-fr/library/microsoft.office.interop.word.documents.open.aspx) –

+0

它不是Open方法的參數,它是Excel.Application本身的屬性:http ://msdn.microsoft.com/en-us/library/bb177478%28v=office.12%29.aspx –

+0

有沒有DisplayAlerts字(http://msdn.microsoft.com/en-us/library/ ff198329.aspx)。但是,當您閱讀文檔時,它指出「如果Microsoft Excel在**宏**運行時顯示某些警報和消息,則爲True」。我認爲這不適用於我的情況。無論如何,我投票贊成關於如何調用複雜參數的提示。 –

0

我現在知道這是舊的,但以防萬一它可以幫助別人,你可以嘗試添加:

$ word.visible =假

$ word.DisplayAlerts = 「wdAlertsNone」

相關問題