Automating Microsoft Office Applications
Introduction
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, and so forth.
Steps to Follow
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
InitInstancefunction, add the following lines: - 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:
- Add the following variables in your dialog's header file.
- To start PowerPoint, you have to write this code in the Start button's function.
- To open a presentation from the hard disk, add this code in the Open button's function call.
- To close PowerPoint, add this code in the Close button's function call.
- To run the slideshow, use this code in the Run button's function call
- Sometimes, you might want to start all over from the first slide. To go to the first slide, you can use this code:
- And similarly, to go to the last slide
- 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:
- Interested in going to the next slide now? In that case, this function will help you:
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
#include "msppt8.h"
_Application app; // app is the PowerPoint
// _Application object
Presentations Presentations;
_Presentation Presentation;
SlideShowView View;
SlideShowWindow SlideShowWindow;
SlideShowSettings slideshow;
Slides slides;
_Slide slide;
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!");
}
}
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);
}
}
void CPowerPntDlg::OnBtnClose()
{
if (CanExit())
app.Quit();
}
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();
}
void CPowerPntDlg::OnBtnFirst()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.First();
}
void CPowerPntDlg::OnBtnLast()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Last();
}
void CPowerPntDlg::OnBtnPrevious()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Previous();
}
void
Conclusion
That's it, folks. Check out the other functions available for transitions, animations, and so forth and you can go ahead on your own. This is the basic framework and you can see how easy it is to handle PowerPoint. It's the same case with Excel, Word, or any other Microsoft Office application. All luck to you and have a great time. You can also check out http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178749 for more information.
I used this code to do remote PowerPoint presentations. The concept was to run a presentation at one place and the clients will be looking at it simultaneously from different places at the same time.

Comments
Add a command button to a slide and set it caption?
Posted by hacaonguyen on 08/16/2005 05:46amDear,
ReplyI added a command button to a slide as follow:
////////////////////
//MyCode Begin
oShape = oShapes.AddOLEObject(10, 10, 100, 50, "Forms.CommandButton.1", "", 0, "", 0, "", );
oShape.SetName("cmdRun");
//MyCode End
/////////////////////////
But my broplem is I could not set caption and VB code for this button.
I can do this in VB as below code:
'''''''''''''''''''''''''
'MyCode in VB Begin
Set oShape = oSlide.Shapes.AddOLEObject(200, 200, 100, 0, "Forms.CommandButton.1")
oShape.OLEFormat.Object = True
oShape.OLEFormat.Object.Name = "cmdRun" 'or oShape.Name = "cmdRun"
oShape.OLEFormat.Object.Caption = "Run"
Dim sCode As String
sCode = "Private Sub " &
oShape.OLEFormat.Object.Name & "_Click()" &
vbCrLf & _
" MsgBox ""You Clicked the
CommandButton""" & vbCrLf & _
"End Sub"
oPresent.VBProject.VBComponents
(1).CodeModule.AddFromString sCode
'MyCode in VB End
''''''''''''''''''''''''
Could any one help.
Many thanks
In C++ how to detect an Automatic Date in a powerpoint slide?
Posted by one_apple on 03/14/2005 02:26pmI will appreciate anybody's help on this: In C++ how to detect an Automatic Date in a powerpoint slide? Thanks
ReplyI have a problem
Posted by Legacy on 02/06/2004 12:00amOriginally posted by: Jenny
Very cool your explanation, but nowdays we have another problem.
ReplyU know all the Sun-Microsoft problem....
From september on there will be no support anymore for the Java packages Microsoft created and that includes the ones used to automate Office. Please, tell me! How would be possible to automate it without using COM stuff, I mean without using the packages Microsoft created? Please, help me! I've been wondering around with no practical solution and u seem very practical!
Thanx on advance!
how to create an acticex control and insert it to .ppt
Posted by Legacy on 12/02/2003 12:00amOriginally posted by: david
i create my activex control,
i use it in .htm correctly.
now , i want to use my activex control in a .ppt file
how can i do for this?
please help me
thanks
david
Replya small question
Posted by Legacy on 11/25/2003 12:00amOriginally posted by: padma
ReplyI Compliment You On Sharing
Posted by Legacy on 08/26/2003 12:00amOriginally posted by: Dave Busey
Thank you for setting out such a nice tutorial. The code and explanations are going to save some of us a lot of time from reference searching and experimentation. I appreciate your generosity.
Dave Busey
ReplyGreat pice of work.
Posted by Legacy on 08/22/2003 12:00amOriginally posted by: Ilya Kalujny
Could be really useful sometimes.
ReplyCool, but is it possible to remote to another machine?
Posted by Legacy on 08/20/2003 12:00amOriginally posted by: Jackson Lee
Your code works on the same machine, is it possible CreateDispatch on another machine?
thx.
lee
ReplyGood info But.....
Posted by Legacy on 08/19/2003 12:00amOriginally posted by: one4all
Good Article, but, I didn't understand why did you need this application for "Remote power presentation".
You could have peacefully used Netmeeting right?
Reply