Forum Discussion

socc3rpr0's avatar
socc3rpr0
Occasional Contributor
11 years ago

Crop Picture with Excel 2007 via COM Server

Hello, 



I can seem to figure out how to crop a picture after I insert it from a location onto a worksheet. I can get the code to run in VB macro, but not with TestComplete. Can someone guide me to the place where I can get more info or see anything wrong?



function InsertPicture(Location)

{

     var MsExcel = Sys["OleObject"]("Excel.Application");

     //This works 

     MsExcel["ActiveSheet"]["Pictures"]["Insert"](Location);

     /**This Doesn;t work

    MsExcel["ActiveSheet"]["Shape"]["Select"]["ShapeRange"]["Height"] = 300;

}



I got this to work in VB 




ActiveSheet.Pictures.Insert(Location)


ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Select


With Selection


.ShapeRange.Height = 300


.ShapeRange.Width = 400


End with 






  • Hi Ed Gonzales,


     


    Try using following functions,


     


    function Test() {


    crop_image("C:\\Dev_Chaminda\\QA\\Automation_Testing\\TestComplete\\TestData\\UserName.xls","Sheet2",50,500)


    }


    function crop_image(filepath,sheetname,heightval,widthval){


    var excel=new ActiveXObject("Excel.Application");


    excel.DisplayAlerts = false; 


    var excel_file = excel.Workbooks.Open(filepath);


    var excel_sheet = excel_file.Worksheets(sheetname);


    excel_sheet.Pictures.ShapeRange.Height = heightval


    excel_sheet.Pictures.ShapeRange.Width = widthval


    excel_file.Save;


    excel_file.Close;


    excel.Exit;


    }

2 Replies

  • socc3rpr0's avatar
    socc3rpr0
    Occasional Contributor
    Thanks for your help this is what I came up with....










    function CropImageFromFile(Location, SheetName, Height, Width)


    {


        //Select WorkSheet  


        SelectWorkSheet(SheetName);


        


        //Insert Picture


        MsExcel["ActiveSheet"]["Pictures"]["Insert"](Location); 


       


        //Crop Image 


        MsExcel["Worksheets"](SheetName)["Pictures"]["ShapeRange"]["Height"] = Height;


        MsExcel["Worksheets"](SheetName)["Pictures"]["ShapeRange"]["Width"] = Width;


    }




     


    function MovePicture(row, column)


    {


         var TargetCellLeft, TargetCellTop;


         var ImageCount;


         


         //Select Target Cell Function to select working Cell 


         ActivateCell(row, column);


         


         //Get Cordinates for target Cell


         TargetCellLeft = MsExcel["ActiveCell"]["Left"];


         TargetCellTop = MsExcel["ActiveCell"]["Top"];


      


         //Set Current Image Postion to Reference 0 base on image count


         ImageCount = MsExcel["ActiveSheet"]["Shapes"]["Count"];


         MsExcel["ActiveSheet"]["Shapes"]("Picture " + ImageCount)["Left"] = 0;


         MsExcel["ActiveSheet"]["Shapes"]("Picture " + ImageCount)["Top"] = 0;


         


         //Move Image to Target Cell 


         MsExcel["ActiveSheet"]["Shapes"]("Picture " + ImageCount)["Left"] = TargetCellLeft;


         MsExcel["ActiveSheet"]["Shapes"]("Picture " + ImageCount)["Top"] =TargetCellTop; 


    }








  • Hi Ed Gonzales,


     


    Try using following functions,


     


    function Test() {


    crop_image("C:\\Dev_Chaminda\\QA\\Automation_Testing\\TestComplete\\TestData\\UserName.xls","Sheet2",50,500)


    }


    function crop_image(filepath,sheetname,heightval,widthval){


    var excel=new ActiveXObject("Excel.Application");


    excel.DisplayAlerts = false; 


    var excel_file = excel.Workbooks.Open(filepath);


    var excel_sheet = excel_file.Worksheets(sheetname);


    excel_sheet.Pictures.ShapeRange.Height = heightval


    excel_sheet.Pictures.ShapeRange.Width = widthval


    excel_file.Save;


    excel_file.Close;


    excel.Exit;


    }