0

Where did I go wrong here? I am trying to copy the chart from excel and save it to a local so I can insert that chart pic into an HTML e-mail. It is coping as I can paste it, but each time it runs I still get the Clipboard contains no image" error

Set-ExecutionPolicy Unrestricted -Scope CurrentUser

$connectstring = "Connection"

$path = "C:?blah blah blah"


Set-ExecutionPolicy Unrestricted
#Temp File Path 
 $pathtmp = "C:Temp path"

#Archive 
 $patharc = "C:Temp patharc"
 
#Set the output File Name
 $name = "Same_Image_test"

#Local Path 
 $localpath ="C:\PowerShellScripts\"

 $timer = (Get-Date -Format MM-dd-yyy-hh-mm)
 $errorlog = $localpath + "Logs\" + $name + ".log"
 $filename = $path + $name + ".csv"
 $filenametemp = $localpath + "CSVOutput\" + $name + "-" +$timer + "-temp.csv"
 $localfilename = "Q:\Temp.xlsx"
 $archive = $patharc + $name + "-archive-from-" + $timer + ".csv"
 
 #Start Logging
 Start-Transcript -Path $errorlog -Append

Add-Type -AssemblyName System.Windows.Forms

 set-itemProperty -Path $localfilename -Name IsReadOnly -Value $false

$app = New-Object -comobject Excel.Application
$app.Visible = $True

##IS NOT READONLY?
 set-itemProperty -Path $localfilename -Name IsReadOnly -Value $false
$wb = $app.Workbooks.Open("Q:\temp.xlsx")
Set-Clipboard
## WAIT ONE MIN
Start-Sleep 20
$wb.Name
$wb.RefreshAll()
Start-Sleep 20
$wb.RefreshAll()
#Copy Chart
$ws = $wb.worksheets.item("Chart")
$image = $ws.ChartObjects().copyPicture()
#$chart = $ws.Selection.PasteSpecial()

Start-Sleep 5


$wb.Save()
$wb.Close()
$app.Quit()

#Copy Chart
$ws = $wb.worksheets.item("Chart")
$image = $ws.ChartObjects().copyPicture()



Edited to add code before I try and paste the image

Markiisrad
  • 11
  • 2
  • Do you need a delay between when you `GetImage()` and when you attempt to retrieve it? – mojo Sep 29 '21 at 17:47
  • Thanks foe the suggestion, I put a Start-Sleep 20 in there and it did not change it. – Markiisrad Sep 29 '21 at 18:08
  • I am starting to wonder if it's related to STAThreadAttribute, but I know nothing about it. – Markiisrad Sep 29 '21 at 18:09
  • What about using `Get-Clipboard -Format Image`? (https://stackoverflow.com/a/55226209/2092609) When I ALT+PrtScr, I'm able to get the image data with that cmdlet. – mojo Sep 29 '21 at 18:58
  • It's still not seeing it, I get "You cannot call a method on a null-valued expression." but the pic in the clipboard as I can paste it elsewhere. – Markiisrad Sep 29 '21 at 20:34
  • Can you post something of the code leading up to `$ws = $wb.worksheets.item("Chart")`? It seems like "getting" the image is the problem, not retrieving it from the clipboard. – mojo Sep 30 '21 at 13:11
  • Sure thing, I edited the post to show everything up until trying to paste the image – Markiisrad Sep 30 '21 at 16:57

0 Answers0