user3657339
user3657339

Reputation: 627

Passing filepath parameters to function

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

Upvotes: 0

Views: 178

Answers (1)

TobyU
TobyU

Reputation: 3928

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"

Upvotes: 2

Related Questions