I am creating a library of powershell functions to manipulate excel (find cell with matching value in collection, copy range, export sheet as collection etc.). I need to be able to pass in parameters and return a datatable so these functions can interact with BluePrism via C#/VB. I am testing calling these functions from BluePrism but whenever I leave a function it breaks my connection to the excel object.
I've been using functions to manipulate an excel COM object and this works as long as 1) my functions are in the same script 2) my access modifiers are set to script. However, I want to be able to call these functions individually from BluePrism.
When I break out of the initializing function (that activates the worksheet/workbook/etc), it breaks my connection to excel.
Is there a way to reconnect to a COM object or somehow keep it active? If not, what other approaches could I use manipulate excel across multiple functions?
Below is an example of two functions I'm trying to run, one that opens and activates an excel sheet and one that returns a particular cell value:
function Open_ExcelSheet{
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[hashtable]
$parms
)
process{
try{
$excelPath = if($null -ne $parms.excelPath) {$parms.excelPath} else {throw "No path specified"}
$sheetName = if($null -ne $parms.sheetName) {$parms.sheetName} else {throw "No sheet specified"}
$visible = if($null -ne $parms.isVisible) {$parms.isVisible} else { $false }
<#Load excel file and navigate to sheet#>
$excel = New-Object -ComObject Excel.Application
if($visible -eq $true){
$excel.Visible = $True
}
$workbook = $excel.Workbooks.Open($excelPath)
$worksheet = $workbook.Worksheets.Item($sheetName)
$worksheet.Activate()
return "True"
}
catch{
return "False"
}
}
function Get_ExcelCell{
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[hashtable]
$parms
)
process{
try{
$excelPath = if($null -ne $parms.excelPath) {$parms.excelPath} else {throw "No path specified"}
$sheetName = if($null -ne $parms.sheetName) {$parms.sheetName} else {throw "No sheet specified"}
$searchValue = if($null -ne $parms.searchValue){$parms.searchValue} else {throw "No search value specified"}
<#Load excel file and navigate to sheet#>
$workbook = $excel.Workbooks.Open($excelPath)
$worksheet = $workbook.Worksheets.Item($sheetName)
$worksheet.Activate()
$Found = $worksheet.Cells.Find($searchValue)
$cellInfo = [PSCustomObject]@{
Row = $Found.Row()
Column = $Found.Column()
Value = $Found.Value()
}
$a = $cellInfo | ConvertTo-DbaDataTable -EnableException
$cellInfo | ConvertTo-DbaDataTable -EnableException -OutVariable dt
return "True"
}
catch {
return "False"
}
}
}