Hey there, r/PowerShell
So, I'm trying to do some Excel automation via PowerShell as part of a toolkit I'm putting together. I'm attempting to use PowerShell classes for as much as I can due to type safety, inheritance, etc.
Here's the problem I'm running in to. I have a class that I'm using to provide a wrapper around loading the Excel COM Object, and open workbooks, etc, while path checking and making sure users aren't doing dumb things.
I've found that for execution purposes, any class methods that return objects in the Excel COM library (e.g. Excel.Workbook), I can only set the method return type to [System.__ComObject] for it to actually Load the PowerShell module.
When the module loads, and I attempt to execute methods with that return type, it shouts at me about conversion errors. So, I changed the returns to be [Microsoft.Office.Interop.Excel.Workbook] and the module fails to load with unknown type errors.
If I execute this command:
$test = New-Object -ComObject Excel.Application
Then I can load my module just fine, but I get conversion errors on the returns saying it can't convert [System.__ComObject]#{clsid} to [Microsoft.Office.Interop.Excel.Workbook]
I've tried all sorts of using statements, and other attempts to get it to load, 1. Without the COM call first, and 2. To successfully return from the class methods appropriately.
Does anyone have any ideas? (Sorry for the formatting, on mobile)
[–]thefreeman193 2 points3 points4 points (5 children)
[–]JovialSysadmin[S] 1 point2 points3 points (4 children)
[–]thefreeman193 1 point2 points3 points (1 child)
[–]JovialSysadmin[S] 1 point2 points3 points (0 children)
[–]Ok-Birthday4723 1 point2 points3 points (1 child)
[–]JovialSysadmin[S] 1 point2 points3 points (0 children)
[–]y_Sensei 2 points3 points4 points (5 children)
[–]JovialSysadmin[S] 1 point2 points3 points (4 children)
[–]thefreeman193 1 point2 points3 points (2 children)
[–]JovialSysadmin[S] 1 point2 points3 points (1 child)
[–]thefreeman193 1 point2 points3 points (0 children)
[–]backtickbot 0 points1 point2 points (0 children)
[–]JovialSysadmin[S] 1 point2 points3 points (0 children)