View Full Version : Get serious trouble in Powerpoint automation code


Zhaohui Xing
02-23-2006, 10:26 PM
I create a Powerpoint automation test program following

How to create an automation project using MFC and a type library
But get serious trouble when it parses only a single rectange shape in a
simple single PPT slide

The proble is always get assertion in "pptApp.put_Visible(false);"
"long nCount = shapepts.get_Count(); "
" long n = gshapes.get_Count(); "

Also I get assertion if I call shape.get_Vertice()

I ugently need the help to solve this problem.

the code is:

#include "stdafx.h"
#include "testPPT.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include <afxdisp.h>

#include "CPresentation.h"
#include "CApplication.h"
#include "CShapeNode.h"
#include "CSlides.h"
#include "CShapeNodes.h"

#include "CDocumentWindow.h"
#include "CPresentations.h"
#include "CShapes.h"
#include "CPageSetup.h"

#include "CFreeformBuilder.h"
#include "CPictureFormat.h"
#include "CShapeRange.h"
#include "CColorScheme.h"
#include "CMaster.h"
#include "CDesign.h"
#include "CHyperlinks.h"
#include "CActionSetting.h"
#include "CTextFrame.h"
#include "CSlide.h"
#include "CShape.h"
#include "CGroupShapes.h"

#include "CColorFormat.h"

#ifndef POWERPOINT_NOTINSTALLED
#define POWERPOINT_NOTINSTALLED 0
#endif

#ifndef POWERPOINT95
#define POWERPOINT95 1
#endif

#ifndef POWERPOINT97
#define POWERPOINT97 2
#endif

#ifndef POWERPOINT2000
#define POWERPOINT2000 3
#endif

#ifndef POWERPOINT2002
#define POWERPOINT2002 4
#endif

#ifndef POWERPOINT2003
#define POWERPOINT2003 5
#endif

//??????????????????????????????
//The Powerpoint conversion error code -- 2006-02-16, zxing
#define PPTCONV_OK 0 //OK
#define PPTCONV_NOPPT 1 //No Powerpoint installed
#define PPTCONV_NOPPTFILE 2 //No Powerpoint file
#define PPTCONV_PPTNOTRUN 3 //Cannot run powerpoint
#define PPTCONV_WRONGPPTVER 4 //Wrong Powerpoint version
//??????????????????????????????

// The one and only application object

CWinApp theApp;

using namespace std;

const OLECHAR FAR* szPPTProgID[] =
{
OLESTR("PowerPoint.Application.7"), // PowerPoint 95
OLESTR("PowerPoint.Application.8"), // PowerPoint 97
OLESTR("PowerPoint.Application.9"), // powerPoint 2000
OLESTR("PowerPoint.Application.10"), // powerPoint 2002
OLESTR("PowerPoint.Application.11"), // powerPoint 2003
};

long QueryPPTVersion(void)
{
long nRet = POWERPOINT_NOTINSTALLED;

CLSID clsid;
for (long i = POWERPOINT95; i <= POWERPOINT2003; i++ )
{
//Query class ID from system
if (SUCCEEDED(CLSIDFromProgID(szPPTProgID[i-1], &clsid)))
{
return i;
}
}
return nRet;
}

bool PPTStartup(CApplication& pptApp, long nVer, bool bShow, long& nPrevWS)
{
bool bRet = false;

if(nVer < POWERPOINT95 || POWERPOINT2003 < nVer)
{
return bRet;
}

CLSID clsid;

if(!SUCCEEDED(CLSIDFromProgID(szPPTProgID[nVer-1], &clsid)))
{
return false;
}

if(!pptApp.CreateDispatch(clsid))
{
TRACE(_T("Couldn't start PowerPoint and get Application object."));
return bRet;
}

nPrevWS = pptApp.get_WindowState();

if(bShow)
{
pptApp.put_Visible(true);
}
else
{
pptApp.put_Visible(false); //Failed get assertion
}

bRet = true;
return bRet;
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}

nRetCode = OleInitialize(NULL);

long nVer = QueryPPTVersion();
long nPrevWS;
bool bShow;

CApplication pptApp;

if(!PPTStartup(pptApp, nVer, true, nPrevWS))
{
ASSERT(FALSE);
}

CPresentations pptPresentations = pptApp.get_Presentations() ;

CPresentation pptPresentation;
pptPresentation = pptPresentations.Open(_T("C:\\Dev\\outline.ppt"),
FALSE, TRUE, TRUE);


CSlides pptSlides;
pptSlides = pptPresentation.get_Slides();
CSlide crtSlide;
crtSlide = pptSlides.Item(COleVariant((long)1));

CShapes shapes = crtSlide.get_Shapes();
CShape shape;
shape = shapes.Item(COleVariant((long)1));

CShapeNodes shapepts = shape.get_Nodes();

long nCount = shapepts.get_Count(); //Get Assertion

CGroupShapes gshapes = shape.get_GroupItems();

long n = gshapes.get_Count(); //Get Assertion

if(pptPresentation.m_lpDispatch != NULL)
{
pptPresentation.Close();
pptPresentation.ReleaseDispatch();
pptPresentation.m_lpDispatch = NULL;
}

if(pptApp.m_lpDispatch != NULL)
{
pptApp.Quit();
pptApp.ReleaseDispatch();
pptApp.m_lpDispatch = NULL;
}

return nRetCode;
}

Steve Rindsberg
02-24-2006, 12:51 AM
I don't do C but it seems you're trying to get properties of a rectangle that
don't exist.

Rectangles don't have a nodes collection and there are no vertices.

A shape that's not .Type = 6 (group) won't have GroupItems and because of that,
no GroupItems.Count



CShapeNodes shapepts = shape.get_Nodes();
long nCount = shapepts.get_Count(); //Get Assertion
CGroupShapes gshapes = shape.get_GroupItems();
long n = gshapes.get_Count(); //Get Assertion

In article <uJvapgMOGHA.964@tk2msftngp13.phx.gbl>, Zhaohui Xing wrote:
> I create a Powerpoint automation test program following
>
> How to create an automation project using MFC and a type library
> But get serious trouble when it parses only a single rectange shape in a
> simple single PPT slide
>
> The proble is always get assertion in "pptApp.put_Visible(false);"
> "long nCount = shapepts.get_Count(); "
> " long n = gshapes.get_Count(); "
>
> Also I get assertion if I call shape.get_Vertice()
>
> I ugently need the help to solve this problem.
>
> the code is:
>
> #include "stdafx.h"
> #include "testPPT.h"
> #ifdef _DEBUG
> #define new DEBUG_NEW
> #endif
>
> #include <afxdisp.h>
>
> #include "CPresentation.h"
> #include "CApplication.h"
> #include "CShapeNode.h"
> #include "CSlides.h"
> #include "CShapeNodes.h"
>
> #include "CDocumentWindow.h"
> #include "CPresentations.h"
> #include "CShapes.h"
> #include "CPageSetup.h"
>
> #include "CFreeformBuilder.h"
> #include "CPictureFormat.h"
> #include "CShapeRange.h"
> #include "CColorScheme.h"
> #include "CMaster.h"
> #include "CDesign.h"
> #include "CHyperlinks.h"
> #include "CActionSetting.h"
> #include "CTextFrame.h"
> #include "CSlide.h"
> #include "CShape.h"
> #include "CGroupShapes.h"
>
> #include "CColorFormat.h"
>
> #ifndef POWERPOINT_NOTINSTALLED
> #define POWERPOINT_NOTINSTALLED 0
> #endif
>
> #ifndef POWERPOINT95
> #define POWERPOINT95 1
> #endif
>
> #ifndef POWERPOINT97
> #define POWERPOINT97 2
> #endif
>
> #ifndef POWERPOINT2000
> #define POWERPOINT2000 3
> #endif
>
> #ifndef POWERPOINT2002
> #define POWERPOINT2002 4
> #endif
>
> #ifndef POWERPOINT2003
> #define POWERPOINT2003 5
> #endif
>
> //??????????????????????????????
> //The Powerpoint conversion error code -- 2006-02-16, zxing
> #define PPTCONV_OK 0 //OK
> #define PPTCONV_NOPPT 1 //No Powerpoint installed
> #define PPTCONV_NOPPTFILE 2 //No Powerpoint file
> #define PPTCONV_PPTNOTRUN 3 //Cannot run powerpoint
> #define PPTCONV_WRONGPPTVER 4 //Wrong Powerpoint version
> //??????????????????????????????
>
> // The one and only application object
>
> CWinApp theApp;
>
> using namespace std;
>
> const OLECHAR FAR* szPPTProgID[] =
> {
> OLESTR("PowerPoint.Application.7"), // PowerPoint 95
> OLESTR("PowerPoint.Application.8"), // PowerPoint 97
> OLESTR("PowerPoint.Application.9"), // powerPoint 2000
> OLESTR("PowerPoint.Application.10"), // powerPoint 2002
> OLESTR("PowerPoint.Application.11"), // powerPoint 2003
> };
>
> long QueryPPTVersion(void)
> {
> long nRet = POWERPOINT_NOTINSTALLED;
>
> CLSID clsid;
> for (long i = POWERPOINT95; i <= POWERPOINT2003; i++ )
> {
> //Query class ID from system
> if (SUCCEEDED(CLSIDFromProgID(szPPTProgID[i-1], &clsid)))
> {
> return i;
> }
> }
> return nRet;
> }
>
> bool PPTStartup(CApplication& pptApp, long nVer, bool bShow, long& nPrevWS)
> {
> bool bRet = false;
>
> if(nVer < POWERPOINT95 || POWERPOINT2003 < nVer)
> {
> return bRet;
> }
>
> CLSID clsid;
>
> if(!SUCCEEDED(CLSIDFromProgID(szPPTProgID[nVer-1], &clsid)))
> {
> return false;
> }
>
> if(!pptApp.CreateDispatch(clsid))
> {
> TRACE(_T("Couldn't start PowerPoint and get Application object."));
> return bRet;
> }
>
> nPrevWS = pptApp.get_WindowState();
>
> if(bShow)
> {
> pptApp.put_Visible(true);
> }
> else
> {
> pptApp.put_Visible(false); //Failed get assertion
> }
>
> bRet = true;
> return bRet;
> }
>
> int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
> {
> int nRetCode = 0;
>
> // initialize MFC and print and error on failure
> if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
> {
> // TODO: change error code to suit your needs
> _tprintf(_T("Fatal Error: MFC initialization failed\n"));
> nRetCode = 1;
> }
> else
> {
> // TODO: code your application's behavior here.
> }
>
> nRetCode = OleInitialize(NULL);
>
> long nVer = QueryPPTVersion();
> long nPrevWS;
> bool bShow;
>
> CApplication pptApp;
>
> if(!PPTStartup(pptApp, nVer, true, nPrevWS))
> {
> ASSERT(FALSE);
> }
>
> CPresentations pptPresentations = pptApp.get_Presentations() ;
>
> CPresentation pptPresentation;
> pptPresentation = pptPresentations.Open(_T("C:\\Dev\\outline.ppt"),
> FALSE, TRUE, TRUE);
>
> CSlides pptSlides;
> pptSlides = pptPresentation.get_Slides();
> CSlide crtSlide;
> crtSlide = pptSlides.Item(COleVariant((long)1));
>
> CShapes shapes = crtSlide.get_Shapes();
> CShape shape;
> shape = shapes.Item(COleVariant((long)1));
>
> CShapeNodes shapepts = shape.get_Nodes();
>
> long nCount = shapepts.get_Count(); //Get Assertion
>
> CGroupShapes gshapes = shape.get_GroupItems();
>
> long n = gshapes.get_Count(); //Get Assertion
>
> if(pptPresentation.m_lpDispatch != NULL)
> {
> pptPresentation.Close();
> pptPresentation.ReleaseDispatch();
> pptPresentation.m_lpDispatch = NULL;
> }
>
> if(pptApp.m_lpDispatch != NULL)
> {
> pptApp.Quit();
> pptApp.ReleaseDispatch();
> pptApp.m_lpDispatch = NULL;
> }
>
> return nRetCode;
> }
>

--
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================

Zhaohui Xing
02-24-2006, 01:40 PM
Thanks!

"Steve Rindsberg" <abuse@localhost.com> wrote in message
news:VA.0000220b.c80ad645@localhost.com...
>I don't do C but it seems you're trying to get properties of a rectangle
>that
> don't exist.
>
> Rectangles don't have a nodes collection and there are no vertices.
>
> A shape that's not .Type = 6 (group) won't have GroupItems and because of
> that,
> no GroupItems.Count
>
>
>
> CShapeNodes shapepts = shape.get_Nodes();
> long nCount = shapepts.get_Count(); //Get Assertion
> CGroupShapes gshapes = shape.get_GroupItems();
> long n = gshapes.get_Count(); //Get Assertion
>
> In article <uJvapgMOGHA.964@tk2msftngp13.phx.gbl>, Zhaohui Xing wrote:
>> I create a Powerpoint automation test program following
>>
>> How to create an automation project using MFC and a type library
>> But get serious trouble when it parses only a single rectange shape in a
>> simple single PPT slide
>>
>> The proble is always get assertion in "pptApp.put_Visible(false);"
>> "long nCount = shapepts.get_Count(); "
>> " long n = gshapes.get_Count(); "
>>
>> Also I get assertion if I call shape.get_Vertice()
>>
>> I ugently need the help to solve this problem.
>>
>> the code is:
>>
>> #include "stdafx.h"
>> #include "testPPT.h"
>> #ifdef _DEBUG
>> #define new DEBUG_NEW
>> #endif
>>
>> #include <afxdisp.h>
>>
>> #include "CPresentation.h"
>> #include "CApplication.h"
>> #include "CShapeNode.h"
>> #include "CSlides.h"
>> #include "CShapeNodes.h"
>>
>> #include "CDocumentWindow.h"
>> #include "CPresentations.h"
>> #include "CShapes.h"
>> #include "CPageSetup.h"
>>
>> #include "CFreeformBuilder.h"
>> #include "CPictureFormat.h"
>> #include "CShapeRange.h"
>> #include "CColorScheme.h"
>> #include "CMaster.h"
>> #include "CDesign.h"
>> #include "CHyperlinks.h"
>> #include "CActionSetting.h"
>> #include "CTextFrame.h"
>> #include "CSlide.h"
>> #include "CShape.h"
>> #include "CGroupShapes.h"
>>
>> #include "CColorFormat.h"
>>
>> #ifndef POWERPOINT_NOTINSTALLED
>> #define POWERPOINT_NOTINSTALLED 0
>> #endif
>>
>> #ifndef POWERPOINT95
>> #define POWERPOINT95 1
>> #endif
>>
>> #ifndef POWERPOINT97
>> #define POWERPOINT97 2
>> #endif
>>
>> #ifndef POWERPOINT2000
>> #define POWERPOINT2000 3
>> #endif
>>
>> #ifndef POWERPOINT2002
>> #define POWERPOINT2002 4
>> #endif
>>
>> #ifndef POWERPOINT2003
>> #define POWERPOINT2003 5
>> #endif
>>
>> //??????????????????????????????
>> //The Powerpoint conversion error code -- 2006-02-16, zxing
>> #define PPTCONV_OK 0 //OK
>> #define PPTCONV_NOPPT 1 //No Powerpoint installed
>> #define PPTCONV_NOPPTFILE 2 //No Powerpoint file
>> #define PPTCONV_PPTNOTRUN 3 //Cannot run powerpoint
>> #define PPTCONV_WRONGPPTVER 4 //Wrong Powerpoint
>> version
>> //??????????????????????????????
>>
>> // The one and only application object
>>
>> CWinApp theApp;
>>
>> using namespace std;
>>
>> const OLECHAR FAR* szPPTProgID[] =
>> {
>> OLESTR("PowerPoint.Application.7"), // PowerPoint 95
>> OLESTR("PowerPoint.Application.8"), // PowerPoint 97
>> OLESTR("PowerPoint.Application.9"), // powerPoint 2000
>> OLESTR("PowerPoint.Application.10"), // powerPoint 2002
>> OLESTR("PowerPoint.Application.11"), // powerPoint 2003
>> };
>>
>> long QueryPPTVersion(void)
>> {
>> long nRet = POWERPOINT_NOTINSTALLED;
>>
>> CLSID clsid;
>> for (long i = POWERPOINT95; i <= POWERPOINT2003; i++ )
>> {
>> //Query class ID from system
>> if (SUCCEEDED(CLSIDFromProgID(szPPTProgID[i-1], &clsid)))
>> {
>> return i;
>> }
>> }
>> return nRet;
>> }
>>
>> bool PPTStartup(CApplication& pptApp, long nVer, bool bShow, long&
>> nPrevWS)
>> {
>> bool bRet = false;
>>
>> if(nVer < POWERPOINT95 || POWERPOINT2003 < nVer)
>> {
>> return bRet;
>> }
>>
>> CLSID clsid;
>>
>> if(!SUCCEEDED(CLSIDFromProgID(szPPTProgID[nVer-1], &clsid)))
>> {
>> return false;
>> }
>>
>> if(!pptApp.CreateDispatch(clsid))
>> {
>> TRACE(_T("Couldn't start PowerPoint and get Application
>> object."));
>> return bRet;
>> }
>>
>> nPrevWS = pptApp.get_WindowState();
>>
>> if(bShow)
>> {
>> pptApp.put_Visible(true);
>> }
>> else
>> {
>> pptApp.put_Visible(false); //Failed get assertion
>> }
>>
>> bRet = true;
>> return bRet;
>> }
>>
>> int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
>> {
>> int nRetCode = 0;
>>
>> // initialize MFC and print and error on failure
>> if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(),
>> 0))
>> {
>> // TODO: change error code to suit your needs
>> _tprintf(_T("Fatal Error: MFC initialization failed\n"));
>> nRetCode = 1;
>> }
>> else
>> {
>> // TODO: code your application's behavior here.
>> }
>>
>> nRetCode = OleInitialize(NULL);
>>
>> long nVer = QueryPPTVersion();
>> long nPrevWS;
>> bool bShow;
>>
>> CApplication pptApp;
>>
>> if(!PPTStartup(pptApp, nVer, true, nPrevWS))
>> {
>> ASSERT(FALSE);
>> }
>>
>> CPresentations pptPresentations = pptApp.get_Presentations() ;
>>
>> CPresentation pptPresentation;
>> pptPresentation = pptPresentations.Open(_T("C:\\Dev\\outline.ppt"),
>> FALSE, TRUE, TRUE);
>>
>> CSlides pptSlides;
>> pptSlides = pptPresentation.get_Slides();
>> CSlide crtSlide;
>> crtSlide = pptSlides.Item(COleVariant((long)1));
>>
>> CShapes shapes = crtSlide.get_Shapes();
>> CShape shape;
>> shape = shapes.Item(COleVariant((long)1));
>>
>> CShapeNodes shapepts = shape.get_Nodes();
>>
>> long nCount = shapepts.get_Count(); //Get Assertion
>>
>> CGroupShapes gshapes = shape.get_GroupItems();
>>
>> long n = gshapes.get_Count(); //Get Assertion
>>
>> if(pptPresentation.m_lpDispatch != NULL)
>> {
>> pptPresentation.Close();
>> pptPresentation.ReleaseDispatch();
>> pptPresentation.m_lpDispatch = NULL;
>> }
>>
>> if(pptApp.m_lpDispatch != NULL)
>> {
>> pptApp.Quit();
>> pptApp.ReleaseDispatch();
>> pptApp.m_lpDispatch = NULL;
>> }
>>
>> return nRetCode;
>> }
>>
>
> --
> Steve Rindsberg, PPT MVP
> PPT FAQ: www.pptfaq.com
> PPTools: www.pptools.com
> ================================================
>