1

My question's about the error at saving file at this convert script:

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

As i haven't experience in powershell i don't understand the metod they call at script.

1 Answer 1

2

I'm still new to powershell myself, so can't answer specifically what was causing the error, however I think I found the solution. It seemed to be with only the Excel conversion. The Word and PPT conversions worked flawless. I've changed the code for that portion and it seems to be working now:

$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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.