2017-02-28 180 views
0

我的問題是關於誤差在此轉換腳本保存文件:轉換Excel格式爲PDF - PowerShell的

https://github.com/idf/batch_dump/blob/master/office_convert.ps1

#Error 
Exception when calling "InvokeMember" with "6" argument (s): "Object does not match destination type." 
In the line: 28 character: 1 
+ $workbook = $objExcel.Workbooks.PSBase.GetType().InvokeMember('Open', ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : TargetException 

saving C:\Test\CD_FEV_1.pdf 

You can not call a method on a null value expression. 
In the line: 30 character: 1 
+ $workbook.ExportAsFixedFormat($xlTypePDF, $filepath, $xlQualityStanda ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

You can not call a method on a null value expression. 
In the line: 31 character: 1 
+ [void]$workbook.PSBase.GetType().InvokeMember('Close', [Reflection.Bi ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

因爲我還沒有在PowerShell中的經驗,我不明白他們在腳本中調用metod。

回答

0

我還是新來的PowerShell自己,所以不能回答具體是什麼原因導致的錯誤,但我想我找到了解決辦法。它似乎只與Excel轉換。 Word和PPT轉換工作完美無瑕。我已經改變了代碼的那部分,似乎是現在的工作:

$folderpath = $(get-location) 
Add-type -AssemblyName office 
#Convert Word formats to pdf 
$wdFormatPDF = 17 
$word = New-Object -ComObject word.application 
$word.visible = $false 
$fileTypes = "*.docx","*doc" 
$wordFiles = Get-ChildItem -path $folderpath -include $fileTypes -Recurse 
foreach ($d in $wordFiles) { 
$path = ($d.fullname).substring(0,($d.FullName).lastindexOf(".")) 
"Converting $path to pdf ..." 
$doc = $word.documents.open($d.fullname) 
$doc.saveas([ref] $path, [ref]$wdFormatPDF) 
$doc.close() 
} 
$word.Quit() 
#Convert Excel formats to pdf 
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] 
$excelFiles = Get-ChildItem -Path $folderpath -include *.xls, *.xlsx -recurse 
$objExcel = New-Object -ComObject excel.application 
$objExcel.visible = $false 
foreach($wb in $excelFiles) 
{ 
$filepath = Join-Path -Path $folderpath -ChildPath ($wb.BaseName + ".pdf") 
$workbook = $objExcel.workbooks.open($wb.fullname, 3) 
$workbook.ActiveSheet.PageSetup.Orientation = 2 
$objExcel.PrintCommunication = $false 
$workbook.ActiveSheet.PageSetup.FitToPagesTall = $false 
$workbook.ActiveSheet.PageSetup.FitToPagesWide = 1 
$objExcel.PrintCommunication = $true 
$workbook.Saved = $true 
"saving $filepath" 
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath) 
$objExcel.Workbooks.close() 
} 
$objExcel.Quit() 
#Convert Powerpoint formats to pdf 
$ppFormatPDF = 2 
$ppQualityStandard = 0 
$p = new-object -comobject powerpoint.application 
$p.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue 
$ppFiletypes = "*.pptx","*ppt" 
$ppFiles = Get-ChildItem -path $folderpath -include $ppFiletypes -Recurse 
foreach ($s in $ppFiles) { 
$pppath = ($s.fullname).substring(0,($s.FullName).lastindexOf(".")) 
"Converting $pppath to pdf ..." 
$ppt = $p.presentations.open($s.fullname) 
$ppt.SavecopyAs($pppath, 32) # 32 is for PDF 
$ppt.close() 
} 
$p.Quit() 
$p = $null 
[gc]::collect() 
[gc]::WaitForPendingFinalizers()