自动化 MS-Office 应用程序






4.22/5 (13投票s)
2002年6月29日
2分钟阅读

232708
本教程将帮助您自动化 PowerPoint,并提供自动化其他 MS-Office 应用程序的提示。
引言
本教程将帮助您学习自动化的基础知识。通过这段代码,您可以从您的应用程序控制 PowerPoint。您可以以编程方式打开 PowerPoint,打开任何演示文稿,跳转到您想要的任何幻灯片,运行幻灯片播放等。
步骤如下
通过遵循以下相同的步骤,您可以自动化 Word、Excel 或任何 Microsoft Office 应用程序。
- 创建一个基于对话框的应用程序,并在应用程序向导的第 3 步(共 6 步)中,选择自动化复选框。
- 为“开始”、“运行”、“关闭”、“第一张幻灯片”、“最后一张幻灯片”、“上一张幻灯片”和“下一张幻灯片”功能创建按钮,并相应地使用以下函数。
- 在您的应用程序的
InitInstance
函数中,添加以下行。 - 在您的对话框类中,打开类向导,选择“自动化”选项卡,选择“添加类”...“从类型库”,并从“C:\Program Files\Microsoft Office\Office\”中选择 msppt8.olb。
- 在您的对话框的头文件中,包含以下行。
- 在您的对话框的头文件中添加以下变量。
- 要启动 PowerPoint,您必须在“开始”按钮的函数中编写此代码。
- 要从硬盘打开演示文稿,请将此代码添加到“打开”按钮的函数调用中。
- 要关闭 PowerPoint,请将此代码添加到“关闭”按钮的函数调用中。
- 要运行幻灯片播放,请在“运行”按钮的函数调用中使用此代码。
- 有时,您可能想从第一张幻灯片重新开始。要跳转到第一张幻灯片,您可以使用此代码。
- 同样,要跳转到最后一张幻灯片
- 现在您已经开始播放幻灯片,您显然想在某个时候跳转到上一张幻灯片。为此,您可以使用此代码。
- 想跳转到下一张幻灯片吗?在这种情况下,此函数将帮助您。
void CPowerPntDlg::OnBtnNext() { Presentation = app.GetActivePresentation(); SlideShowWindow = Presentation.GetSlideShowWindow(); View = SlideShowWindow.GetView(); View.Next(); }
// 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();
}
结论
就这样了,各位。查看可用于过渡、动画等其他函数,您可以自行继续。这是基本框架,您可以看到处理 PowerPoint 很容易。Excel、Word 或任何其他 Microsoft Office 应用程序的情况也是如此。祝您好运,玩得开心。您还可以查看 http://support.microsoft.com/default.aspx?scid=kb;en-us;Q178749 以获取更多信息。我使用这段代码进行远程 PowerPoint 演示。