By following the same steps given below, you can automate Word, Excel, or any other Microsoft Office application.
- Create a dialog-based application and in the App Wizard's step 3 of 6, select the automation checkbox.
- Create buttons for Start, Run, Close, First Slide, Last Slide, Previous Slide, and Next Slide functions and use the following functions accordingly.
- In your application's
InitInstance
function, add the following lines:
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
- In your dialog's class, open the class wizard, select the automation tab, select "Add Class" ... "From a type library", and then select msppt8.olb from "C:\Program Files\Microsoft Office\Office\".
- In your header file of your dialog, include the following line:
#include "msppt8.h"
- Add the following variables in your dialog's header file.
_Application app;
Presentations Presentations;
_Presentation Presentation;
SlideShowView View;
SlideShowWindow SlideShowWindow;
SlideShowSettings slideshow;
Slides slides;
_Slide slide;
- To start PowerPoint, you have to write this code in the Start button's function.
void CPowerPntDlg::OnBtnStart()
{
if(!app.CreateDispatch("Powerpoint.Application"))
{
AfxMessageBox("Couldn't start PowerPoint.");
}
else
{
app.SetVisible(TRUE);
TRACE("PowerPoint is Running!");
}
}
- To open a presentation from the hard disk, add this code in the Open button's function call.
void CPowerPntDlg::OnBtnOpen()
{
static char BASED_CODE szFilter[] = "PowerPoint Files
(*.ppt)|*.ppt||";
CFileDialog FileDlg(TRUE,"PPT",NULL,
OFN_FILEMUSTEXIST|OFN_NONETWORKBUTTON|
OFN_PATHMUSTEXIST,szFilter);
FileDlg.DoModal();
CString strFileName;
strFileName = FileDlg.GetPathName();
if(!strFileName.IsEmpty())
{
Presentations = app.GetPresentations();
Presentation = Presentations.Open(strFileName,0,0,1);
}
}
- To close PowerPoint, add this code in the Close button's function call.
void CPowerPntDlg::OnBtnClose()
{
if (CanExit())
app.Quit();
}
- To run the slideshow, use this code in the Run button's function call
void CPowerPntDlg::OnBtnRun()
{
Presentations = app.GetActivePresentation();
slides = Presentation.GetSlides();
slide = slides.Item(COleVariant((long)1));
slideshow = Presentation.GetSlideShowSettings();
slideshow.Run();
}
- Sometimes, you might want to start all over from the first slide. To go to the first slide, you can use this code:
void CPowerPntDlg::OnBtnFirst()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.First();
}
- And similarly, to go to the last slide
void CPowerPntDlg::OnBtnLast()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Last();
}
- Now that you have the slideshow running, you would obviously want to go to the previous slide at some point of time. To do just that, you can use this code:
void CPowerPntDlg::OnBtnPrevious()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Previous();
}
- Interested in going to the next slide now? In that case, this function will help you:
void CPowerPntDlg::OnBtnNext()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Next();
}