Why does Shapes.Item return 0x8004005 error on PowerPoint 2007?

97 views Asked by At

I first tested this code (simple code to create a new file and delete all the shapes) using PowerPoint 2016. It seems to work just fine:

using namespace PowerPoint;

//Load currently running PowerPoint
_ApplicationPtr pptApp;
HRESULT hr = pptApp.GetActiveObject(__uuidof(PowerPoint::Application));

//Create new file with a new slide
_PresentationPtr presEdit = pptApp->Presentations->Add(msoTrue);
_SlidePtr slide = presEdit->Slides->Add(1, PpSlideLayout::ppLayoutText);

//Delete all shapes.
while (slide->Shapes->Count > 0)
{
    //This line seems to give an error in 2007
    PowerPoint::ShapePtr shp = slide->Shapes->Item(1); 
    shp->Delete(); 
}

But when I tested it on Powerpoint 2007, something weird happened. When I called the Shapes->Item method, it threw a _com_error with the error code 0x80004005 (Unspecified Error) with the message Shapes (unknown member) : Failed. I searched for this error message and got nothing. I even tried using the 2007 version typelib file, and that didn't work either. After a while, I decided to just revert to using the IDispatch::Invoke method (which was a pain) and that somehow worked. Not sure why that worked and why my earlier attempts did not.

I'm using #import and smart pointers to import the Microsoft Office library into my code.

0

There are 0 answers