MS Excel COM Issue - Unable to get the image out from Clipboard while using excel COM object

Viewed 27

Unable to get the image out from Clipboard while using excel COM object Range.CopyPicture method

On my local machine everything is working fine but on server I am facing this issue. Local machine I have office 2016 installed and at server we have Excel-2013 installed.

After deployment when i run the web application then it is throwing exception (object ref not to set instance). But when I go to server and manually doing ctrl+v (paste) in any word doc directly then it pasting the image there, But from the code it is not able to read the image.

Code-

     System.Threading.Thread.Sleep(2000);
     xlApp.CutCopyMode = XlCutCopyMode.xlCopy;
     Range range = xlWorkSheet.get_Range(startRange, endRange);                                              
     System.Windows.Forms.Clipboard.Clear();               
                            
     range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
     
     //Here getting null in imgWS1                                     
     Image imgWS1 = System.Windows.Forms.Clipboard.GetImage(); 
                              
     string fileName = Path.Combine(reportPath, imageName);

     //Getting exception- object ref not to set an instance..           
     imgWS1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 

Things I have tried 1.) When I am adding the test image in clipboard through

Forms.Clipboard.SetImage(imgtest);

then

Clipboard.GetImage() 

is working fine. So here I think some problem is with the CopyPicture method.

2.) I also tried using DataObject but had no luck.

 System.Windows.Forms.DataObject o = Forms.Clipboard.GetDataObject() as DataObject;
 System.Windows.Forms.Clipboard.SetImage(GetClipboardImage(o));
0 Answers
Related