none
Update the picture shape in Excel RRS feed

  • Question

  • the environment is VS2010+Office 2010.

    I was wondering if i could use VSTO to update a picture's content. The picture has been previously  added to the worksheet, now i want to display a new picture in the same shape.

    Furthermore, if the picture is part of a group, can I manipulate it w/o ungrouping first?

    Thanks in advance

    Monday, September 10, 2012 9:44 PM

Answers

  • Hi ForeignKey,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that there have no method, property to change Shape's image. You can set a new Shape which have same Top, Left, Width, Height instead of original one. Following snippet will work:

                Excel.Application xlApp = Globals.ThisWorkbook.Application;
                Excel.Workbook xlWorkbook = xlApp.ActiveWorkbook;
                Excel.Worksheet xlWorksheet = xlWorkbook.ActiveSheet;
                Excel.Shapes xlShapes = xlWorksheet.Shapes;
                Excel.Shape xlShape = xlShapes.Item(1);
                if (xlShape != null)
                {
                    float t, l, w, h;
                    t = xlShape.Top;
                    l = xlShape.Left;
                    w = xlShape.Width;
                    h = xlShape.Height;
                    xlShape.Delete();
                    xlShapes.AddPicture(@"C:\*******\untitled1.png", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue,l,t,w,h);
                }


    Furthermore, if the picture is part of a group, can I manipulate it w/o ungrouping first?

    I think I can't understanding it well. Would you please clarify this scenario?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 11, 2012 2:05 AM
    Moderator

All replies

  • Hi ForeignKey,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that there have no method, property to change Shape's image. You can set a new Shape which have same Top, Left, Width, Height instead of original one. Following snippet will work:

                Excel.Application xlApp = Globals.ThisWorkbook.Application;
                Excel.Workbook xlWorkbook = xlApp.ActiveWorkbook;
                Excel.Worksheet xlWorksheet = xlWorkbook.ActiveSheet;
                Excel.Shapes xlShapes = xlWorksheet.Shapes;
                Excel.Shape xlShape = xlShapes.Item(1);
                if (xlShape != null)
                {
                    float t, l, w, h;
                    t = xlShape.Top;
                    l = xlShape.Left;
                    w = xlShape.Width;
                    h = xlShape.Height;
                    xlShape.Delete();
                    xlShapes.AddPicture(@"C:\*******\untitled1.png", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue,l,t,w,h);
                }


    Furthermore, if the picture is part of a group, can I manipulate it w/o ungrouping first?

    I think I can't understanding it well. Would you please clarify this scenario?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 11, 2012 2:05 AM
    Moderator
  • Hi, Tom:

    Thank you for the reply. It seems that I have no choice but to add a new picture and delete the old one

    My 2nd question is to explain that the aforementioned picture shape was grouped together w/ other shapes and should I ungroup them to manipulate the picture shape alone.

    since I need to do the add/delete, i guess ungrouping is unavoidable.

    Thanks again.

    Tuesday, September 11, 2012 2:15 PM
  • Hi ForeignKey,

    I think so. Welcome to share your work round if you can get better solution.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 12, 2012 2:28 AM
    Moderator