tricore Guest
|
Posted: Mon Dec 11, 2006 6:59 am Post subject: How to create new PowerPoint 2003 slides programmatically- |
|
|
- The sample code given below is used to get running instance of an application:
------------------------------------------
::CoInitialize(NULL);
// Translate server ProgID into a CLSID. ClsidFromProgID
// gets this information from the registry.
CLSID clsid;
CLSIDFromProgID(L"PowerPoint.Application", &clsid);
// Get an interface to the running instance, if any..
IUnknown *pUnk;
HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
assert(!FAILED(hr));
// Get IDispatch interface...
IDispatch *pDisp;
hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pDisp);
assert(!FAILED(hr));
// Release the no-longer-needed IUnknown...
pUnk->Release();
// ----------------------------------------------------
// Your code here-
// ----------------------------------------------------
::CoUnintialize();
--------------------------------------------
Files to be included: #include <assert.h>
NOTE: If there are multiple instances running at the same time, the GetActiveObject() API function returns the IDispatch pointer to the instance that was first running. In case of Powerpoint there will be only one instance running at a given time.
Theoretically, you can iterate the ROT for each individual instance, but Office applications do not register themselves if another instance is already in the ROT because the moniker for itself is always the same, and cannot be distinguished. This means that you cannot attach to any instance except for the first. However, because Office applications also register their documents in the ROT, you can successfully attach to other instances by iterating the ROT looking for a specific document, attaching to this document, and then getting the Application object from this document. Note that this solution is not necessary for single-instance applications since those applications can have only one instance running at a given time.
- Used the default classes provided by MSPPT.OLB file and created a sample application at my end for adding new slides.
- Sample code for adding new slides and accessing presentations collection:
CLSID clsid;
HRESULT hr;
IDispatch * pDispApp;
IDispatch * pDispActivePresentation;
IDispatch * pDispSlides;
IDispatch * pDispSlide;
// Get CLSID for PowerPoint
hr = CLSIDFromProgID(L"PowerPoint.Application", &clsid);
if(FAILED(hr))
{
TRACE0("DEBUG 13: PowerPoint not installed!");
return ; // ...they don't even have PowerPoint
}
// Get an interface to the running instance, if there is one
IUnknown *pUnk;
hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
if( !FAILED(hr) && pUnk )
{
hr = pUnk->QueryInterface(IID_IDispatch, (void**) &pDispApp);
pUnk->Release();
}
else
hr = (HRESULT)(-1);
_Application MyApp (pDispApp);
pDispActivePresentation = MyApp.GetActivePresentation();
ASSERT_POINTER(pDispActivePresentation, IDispatch);
IDispatch* pDispPresentations = MyApp.GetPresentations();
ASSERT_POINTER(pDispPresentations, IDispatch);
_Presentation * pPresentations = new _Presentation (pDispActivePresentation);
pDispSlides = pPresentations->GetSlides();
ASSERT_POINTER(pDispSlides, IDispatch);
Slides * pSlides = new Slides (pDispSlides);
LONG m_lPage = pSlides->GetCount()+1;
pDispSlide = pSlides->Add( (long)m_lPage, (long)12 ); //pptLayoutBlank=12
ASSERT_POINTER(pDispSlide, IDispatch);
_Slide * pSlide = new _Slide (pDispSlide);
Note: Unhandled exception error occurs when null reference is passed to the IDispatch variable. |
|