I have hundreds of Excel files and one main file that has links to each of these files, but in order for the data to be pulled into the main excel file I need to have every file opened within the same Excel instance and then work through each one. If Microsoft let me open more than 15 files at a time this would be as easy as it was back in the day to open them all at once, but I understand why this isn't allowed anymore... So I created a script to open all of the files with:
$xl = New-Object -comobject Excel.Application foreach($file in $files){ $wb = $xl.Workbooks.open($filename) } $xl.visible = $true
I am also going to be processing hundreds of rows per file. I don't care about the order the files are opened in necessarily. So I have narrowed in on usingStart-Job like so because of the unordered opening and the overhead of starting the Start-Job is better than the forking process I have tried the following:
Start-Job -ScriptBlock {Param( $filename); Write-Host $filename "*" $input "*"; $input.Workbooks.open($filename) } -inputobject $xl -ArgumentList @($filename) ~my_path_and_filename~ * System.Management.Automation.Runspaces.PipelineReader`1+<GetReadEnumerator>d__0[System.Object] * You cannot call a method on a null-valued expression.+ CategoryInfo : InvalidOperation: (open:String) [], RuntimeException+ FullyQualifiedErrorId : InvokeMethodOnNull
Start-Job -ScriptBlock {Param( $filename); Write-Host $filename "*" $args[0] "*"; $filename.Workbooks.open($args[0]) } -ArgumentList @($xl, $filename) Microsoft.Office.Interop.Excel.ApplicationClass * ~my_path_and_filename~ * Method invocation failed because [Deserialized.System.__ComObject#{000208db-0000-0000-c000-000000000046}] doesn't contain a method named 'open'.+ CategoryInfo : InvalidOperation: (open:String) [], RuntimeException+ FullyQualifiedErrorId : MethodNotFound
Start-Job -ScriptBlock {Param($xl, $filename); Write-Host $filename "*" $xl "*"; $xl.Workbooks.open($filename) } -ArgumentList @($xl, $filename) ~my_path_and_filename~ * Microsoft.Office.Interop.Excel.ApplicationClass * Method invocation failed because [Deserialized.System.__ComObject#{000208db-0000-0000-c000-000000000046}] doesn't contain a method named 'open'. + CategoryInfo : InvalidOperation: (open:String) [], RuntimeException+ FullyQualifiedErrorId : MethodNotFound
So, is there a way to pass an Excel Object to the Start-Job cmdlet? Or is there a way to pass an Excel object out of the Start-Job so the main Object can take it over? And lastly, when receiving a job, does one get access to the actual objects inside of the job?