ImportExcel icon indicating copy to clipboard operation
ImportExcel copied to clipboard

Convert-ExcelRangeToImage throws an error on get-clipboard

Open TWalijew opened this issue 4 years ago • 6 comments

Hello, in Powershell (PSVersion 7.2.6, ImportExcel v7.8.0) I tried to make use of the function Convert-ExcelRangeToImage and ran into issues. The line Convert-ExcelRangeToImage -path ".\test.xlsx" -WorksheetName ABC -Range $range -Destination aaa.png throws an error on

Get-Clipboard: <path>\Documents\PowerShell\Modules\ImportExcel\7.8.0\Public\Convert-ExcelRangeToImage.ps1:26:33  
$image  = Get-Clipboard -Format Image                                                                      
     |                                  ~~~~~~~                                                                            
     | A parameter cannot be found that matches parameter name 'Format'.  

Looking up the MS Get-Clipboard documentation I found that the switch '-format' no longer exists in Powershell version > 5. Best Regards Thomas

TWalijew avatar Aug 25 '22 11:08 TWalijew

Thanks, makes sense, I'll look into it.

dfinke avatar Aug 25 '22 17:08 dfinke

@jhoneill wanted to check with you on this before following the 🐇

dfinke avatar Aug 25 '22 17:08 dfinke

@jhoneill wanted to check with you on this before following the 🐇

OK

TWalijew avatar Aug 26 '22 07:08 TWalijew

@dfinke I had a look at this and @TWalijew is right, PowerShell 6 dropped Get-Clipboard altogether, and PowerShell re-introduced it without -Format.

It is possible to use [System.Windows.Clipboard] - see
https://stackoverflow.com/questions/72567806/use-powershell-to-save-a-system-windows-interop-interopbitmap-to-disk

This function talks to Excel.exe over COM, so it's Windows only, and the function only works with V5 (nothing earlier). Detecting the version and saying "sorry this needs V5", feels lazy. But I think the function isn't used very much and it's not a 2 minute job to fix it. @TWalijew - is this something you use a lot and you're trying to switch over to PowerShell 7, or did you find it and report it as a courtesy thing. If anyone is badly inconvenienced by this I'd say fix it, if it's just ugliness then I'd just do a version check and leave it for now.

jhoneill avatar Aug 26 '22 11:08 jhoneill

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 01 '22 07:10 stale[bot]

Hello, The following is working for powershell 5.1 and powershell 7: Add-Type -Assembly System.Windows.Forms

# Get-Clipboard came in with PS5. Older versions can use [System.Windows.Clipboard] but it is ugly. #$image = Get-Clipboard -Format Image #$image.Save($Destination, $Format) $clipboard = [System.Windows.Forms.Clipboard]::GetDataObject() [System.Drawing.Bitmap]$clipboard.getimage().Save("$Destination", [System.Drawing.Imaging.ImageFormat]::$extension)

Best regards,

PhidarkMM avatar Jun 19 '24 13:06 PhidarkMM