Excel Shapes

Add-in Express™ Support Service
That's what is more important than anything else

Excel Shapes
Getting shape names 
Nicholas Glasier


Guest


I've been trying to get the name of any selected object ( textbox, button, picture, drawing shape or chart object etc) on a worksheet or chart without success. I thought that the ExcelApp.Selection function would return it and then I could get the name with the following:

function TAddInModule.GetSelectedShape: Widestring;
var
Intf: IDispatch;
Shape: IShape;
begin
Intf := ExcelApp.Selection[adxLCID];
Intf.QueryInterface(IShape, Shape);
Shape.Get_Name(Result);
end;

I used Get_Name because Ishape doesn't expose the name, but although it compiles correctly, it causes an exception. What is the correct way to get this info?

Nick
Posted 08 Mar, 2005 02:16:40 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Nick,

Try the following code:
function TAddInModule.GetSelectedShape: Widestring;
var
  dispShape: Shape;
  dispShapeRange: ShapeRange;
  V: OleVariant;
begin
  Result := '';
  try
    try
      V := OleVariant(ExcelApp.Selection[adxLCID]).ShapeRange;
      dispShapeRange := ShapeRange(TVarData(V).VDispatch);
      if Assigned(dispShapeRange) then begin
        dispShape := dispShapeRange.Item(1);
        Result := dispShape.Name;
      end;
    finally
      dispShape := nil;
      dispShapeRange := nil;
      V := Unassigned;
    end;
  except
  end;
end;



I hope this link will be helpful:
http://msdn.microsoft.com/library/en-us/vbaxl10/html/xlobjShape.asp



Posted 08 Mar, 2005 07:26:26 Top
Nicholas Glasier


Guest


Thanks Dmitry,
that works great for objects on a worksheet, but not for parts of a chart, I'll have a play and see if I can get them too. Presumably chart objects are in a different dispinterface to ordinary shapes, although the Vba Excel documentation suggests they are all in the same Selection collection, and all shapes.

Regards Nick
Posted 08 Mar, 2005 17:16:17 Top