发布网友 发布时间:2022-04-22 10:05
共1个回答
热心网友 时间:2023-10-31 18:04
Introction
This tutorial helps you to learn the basics of automation. With this code,
you can control PowerPoint from your application. You can open PowerPoint
programmatically, open any presentation, go to any slide that you want to, run
the slideshow etc.
Steps to follow
<P>By following the same steps given below, you can automate , word, excel or
any Microsoft office application.</P>
<OL>
<LI>Create a dialog based application and in the App-wizard's step 3 of 6,
select the automation checkbox.
<LI>Create buttons for Start , Run, Close, First Slide, Last Slide, Previous
Slide and Next Slide functions and use the following functions accordingly.
<LI>In your application's <CODE>InitInstance</CODE> function , add the following
lines. <PRE>// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}</PRE>
<LI>In your dialog's class , open class-wizard , select the automation tab,
select "Add Class" ... "From a type library" and select <I>msppt8.olb</I> from
"<I>C:\Program Files\Microsoft Office\Office\</I>"
<LI>In your header file of your dialog, include the following line. <PRE>#include "msppt8.h"</PRE>
<LI>Add the following variables in your dialog's header file. <PRE>_Application app; // app is the PowerPoint _Application object
Presentations Presentations;
_Presentation Presentation;
SlideShowView View;
SlideShowWindow SlideShowWindow;
SlideShowSettings slideshow;
Slides slides;
_Slide slide;</PRE>
<LI>To start PowerPoint, you have to write this code in the Start button's
function. <PRE>void CPowerPntDlg::OnBtnStart()
{
// Start PowerPoint and get Application object...
if(!app.CreateDispatch("Powerpoint.Application"))
{
AfxMessageBox("Couldn't start PowerPoint.");
}
else // Make PowerPoint visible and display a message
{
app.SetVisible(TRUE);
TRACE("PowerPoint is Running!");
}
}</PRE>
<LI>To open a presentation from the hard disk, add this code in the Open
button's function call. <PRE>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();
// To get the selected file's path and name
CString strFileName;
strFileName = FileDlg.GetPathName();
if(!strFileName.IsEmpty())
{
Presentations = app.GetPresentations();
Presentation = Presentations.Open(strFileName,0,0,1);
}
}</PRE>
<LI>To close PowerPoint add this code in the Close button's function call. <PRE>void CPowerPntDlg::OnBtnClose()
{
if (CanExit())
app.Quit();
}</PRE>
<LI>To run the slideshow use this code in the Run button's function call <PRE>void CPowerPntDlg::OnBtnRun()
{
Presentations = app.GetActivePresentation();
slides = Presentation.GetSlides();
// Show the first slide of the presentation
slide = slides.Item(COleVariant((long)1));
//Run the show
slideshow = Presentation.GetSlideShowSettings();
slideshow.Run();
}</PRE>
<LI>Sometimes, you might want to start all over from the first slide. To go to
the first slide you can use this code. <PRE>void CPowerPntDlg::OnBtnFirst()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.First();
}</PRE>
<LI>And similarly, to go to the last slide <PRE>void CPowerPntDlg::OnBtnLast()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Last();
}</PRE>
<LI>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. <PRE>void CPowerPntDlg::OnBtnPrevious()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Previous();
}</PRE>
<LI>Interested to go to the next slide now ? In that case, this function will
help you.<PRE>void CPowerPntDlg::OnBtnNext()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Next();
}