2017-02-28 5 views
0

Meine Frage ist über den Fehler in Datei in diesem convert Skript speichern:Excel-Formate 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 

Da ich nicht in Powershell-Erfahrung habe ich nicht verstehen, die Metod rufen sie am Skript an.

Antwort

0

Ich bin noch neu in Powershell selbst, so kann nicht speziell beantworten, was den Fehler verursacht hat, aber ich denke, ich habe die Lösung gefunden. Es schien nur mit der Excel-Konvertierung zu sein. Die Word- und PPT-Conversions funktionierten einwandfrei. Ich habe den Code für diesen Teil geändert und es scheint jetzt zu funktionieren:

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