Passing filepath parameters to function

0

Below mentioned code is giving error, and I'm not able to troubleshoot issue. Can anyone please help, we are just passing SourceFile and DestinationFile location to this function...

ProcessDocumentsData "D:\Files\Scan1.doc","D:\Files\Scan1.csv"

Clear-Host
function ProcessDocumentsData {
    Param(
        [string]$SourceFile,
        [string]$DestinationFile
    )

    $DestinationFileName = $DestinationFile
    $SourceFileName = $SourceFile
    $tableNum = 13

    $delimiter = ','
    $objWord = New-Object -Com Word.Application
    $objWord.Visible = $false # $false
    $objDocument = $objWord.Documents.Open($SourceFileName)
    $LETable = $objDocument.Tables.Item($tableNum)
    $LETableCols = $LETable.Columns.Count
    $LETableRows = $LETable.Rows.Count

    $RawCSV = for($r=1; $r -le $LETableRows; $r++) {
        $content= @()
        for($c=1; $c -le $LETableCols; $c++) {
            #Write-Host ("R:{0},C:{1}" -f $r,$c)
            $content += ("`"{0}`"" -f $LETable.Cell($r,$c).Range.Text -replace "(`r|`n|`t)|$([char]7)?")
        }
        $Content -join $delimiter
    }
    $Csv = $RawCSV | ConvertFrom-Csv
    $objDocument.Close()
    $objWord.Quit()
    # Stop Winword Process
    $rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)
    $Csv
    $Csv | Export-Csv $DestinationFileName -NoTypeInformation
}

I'm getting the following error:

ProcessDocumentsData "D:\Files\Scan1.doc","D:\Files\Scan1.csv"

Command failed
At line:7 char:1
+ $objDocument = $objWord.Documents.Open($filename)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

You cannot call a method on a null-valued expression.
At line:8 char:1
+ $LETable = $objDocument.Tables.Item($tableNum)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:21 char:1
+ $objDocument.Close()
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
powershell
asked on Stack Overflow Jul 18, 2018 by user3657339 • edited Jul 19, 2018 by Ansgar Wiechers

1 Answer

2

In PowerShell, when calling a function multiple parameters are delimited by a whitespace.

Try to call the function like that:

ProcessDocumentsData "D:\Files\Scan1.doc" "D:\Files\Scan1.csv"
answered on Stack Overflow Jul 18, 2018 by TobyU

User contributions licensed under CC BY-SA 3.0