使用的 Microsoft 基础类 (MFC) 自动完成 Microsoft PowerPoint [草稿]
本文介绍如何通过使用 Visual c + + 5.0 或 Visual c + + 6.0 中使用的 Microsoft 基础类 (MFC) 自动完成 Microsoft PowerPoint。
更多信息
通过使用自动化在 PowerPoint 中,您可以以编程方式打印、 显示幻灯片,和执行大多数您可以以交互方式执行该操作。 请按照下列步骤以生成并运行自动化示例:
- 创建一个新的基于对话框的 MFC EXE 项目。
- 将按钮添加到您的对话框和 BN_CLICKED 处理程序中,为它。
- 打开 类向导 (Ctrl + W),单击 自动化 选项卡、 单击 添加类,并选择 从类型库。
我的话,在2005中,项目---添加类----mfc----activex控件中的mfc类----添加------文件------找到下文位置,如果没有这个olb文件,可以从网上下载一个并拷贝到指定位置。
- 转到目录 (例如对于是 Files\Microsoft Office\Office) Office 的安装位置,然后选择 Msppt8.olb。PowerPoint 对象库中的 PowerPoint 2000 命名 Msppt9.olb。PowerPoint 对象库中的 PowerPoint 2002 是 Msppt.olb,它位于,通过 c:\Program Files\Microsoft Office\Office10 文件夹中的默认值。PowerPoint 对象库中的 Microsoft Office PowerPoint 2003 是 Msppt.olb,它位于,通过 c:\Program Files\Microsoft Office\Office11 文件夹中的默认值
我的话:
在在对话框应用程序的头文件中添加如下变量:
_Application app;
Presentations Presentations;
_Presentation Presentation;
SlideShowView View;
SlideShowWindow SlideShowWindow;
SlideShowSettings slideshow;
Slides slides;
_Slide slide;
- 选择查找的所有类,然后单击 确定 以回到您的项目。类向导已从 PowerPoint 类型库生成一些自动化"包装类",并创建 Msppt8.h 和 Msppt8.cpp 文件。
- 下面的代码添加到按钮处理程序:
// Start PowerPoint. _Application app; COleException e; if(!app.CreateDispatch("Powerpoint.Application", &e)) { CString str; str.Format("CreateDispatch() failed w/err 0x%08lx", e.m_sc), AfxMessageBox(str, MB_SETFOREGROUND); return; } // Make it visible. app.SetVisible(TRUE); // Get Presentations collection and add a new presentation. Presentations presSet(app.GetPresentations()); _Presentation pres(presSet.Add(TRUE)); // Get Slides collection and add a new slide. Slides slideSet(pres.GetSlides()); _Slide slide1(slideSet.Add(1, 2)); // Add text to slide, by navigating the slide as follows: // slide1.shapes(#).TextFrame.TextRange.Text { Shapes shapes(slide1.GetShapes()); Shape shape(shapes.Item(COleVariant((long)1))); TextFrame textFrame(shape.GetTextFrame()); TextRange textRange(textFrame.GetTextRange()); textRange.SetText("My first slide"); } { Shapes shapes(slide1.GetShapes()); Shape shape(shapes.Item(COleVariant((long)2))); TextFrame textFrame(shape.GetTextFrame()); TextRange textRange(textFrame.GetTextRange()); textRange.SetText("Automating PowerPoint is easy\r\n" "Using Visual C++ is powerful!"); } // Add another slide with a chart. _Slide slide2(slideSet.Add(2, 5)); // Add text to slide as before. { Shapes shapes(slide2.GetShapes()); Shape shape(shapes.Item(COleVariant((long)1))); TextFrame textFrame(shape.GetTextFrame()); TextRange textRange(textFrame.GetTextRange()); textRange.SetText("Slide 2's topic"); } { Shapes shapes(slide2.GetShapes()); Shape shape(shapes.Item(COleVariant((long)2))); TextFrame textFrame(shape.GetTextFrame()); TextRange textRange(textFrame.GetTextRange()); textRange.SetText("You can create and use charts " "in your PowerPoint slides!"); } // Add a chart where the default one was created. { // First get coordinates of old chart. float cTop, cWidth, cHeight, cLeft; Shapes shapes(slide2.GetShapes()); Shape shape(shapes.Item(COleVariant((long)3))); cTop = shape.GetTop(); cWidth = shape.GetWidth(); cHeight = shape.GetHeight(); cLeft = shape.GetLeft(); // Delete original chart. shape.Delete(); // Now add your own back where old one was. Shape tmpShape(shapes.AddOLEObject(cLeft, cTop, cWidth, cHeight, "MSGraph.Chart", "", 0, "", 0, "", 0)); } // Add another slide, with an Organization chart. _Slide slide3(slideSet.Add(3, 7)); // Add text to slide as before. { Shapes shapes(slide3.GetShapes()); Shape shape(shapes.Item(COleVariant((long)1))); TextFrame textFrame(shape.GetTextFrame()); TextRange textRange(textFrame.GetTextRange()); textRange.SetText("The rest is only limited by your Imagination"); } // Add a chart where the default one was created. { // First get coordinates of old chart. float cTop, cWidth, cHeight, cLeft; Shapes shapes(slide3.GetShapes()); Shape shape(shapes.Item(COleVariant((long)2))); cTop = shape.GetTop(); cWidth = shape.GetWidth(); cHeight = shape.GetHeight(); cLeft = shape.GetLeft(); // Delete original chart. shape.Delete(); // Now add your own back where old one was. // The next line assumes you have the Microsoft OrgChart application // installed and registered on your computer. Shape tmpShape(shapes.AddOLEObject(cLeft, cTop, cWidth, cHeight, "OrgPlusWOPX.4", "", 0, "", 0, "", 0)); } // Setup slide show properties. for(int i=1; i<=3; i++) { _Slide slide(slideSet.Item(COleVariant((long)i))); SlideShowTransition sst(slide.GetSlideShowTransition()); sst.SetEntryEffect(513); // Random. sst.SetAdvanceOnTime(TRUE); sst.SetAdvanceTime(5.0); // 5-seconds per slide. } // Prepare and run a slide show. { SlideShowSettings sss(pres.GetSlideShowSettings()); sss.SetShowType(3); // Kiosk. sss.SetLoopUntilStopped(TRUE); sss.SetRangeType(1); // Show all. sss.SetAdvanceMode(2); // Use slide timings. SlideShowWindow ssw(sss.Run()); // Run show. } // Sleep so user can watch slide show. ::Sleep(15000); // Tell PowerPoint to quit. app.Quit();
- 实施按钮处理程序的前面添加以下行:
#include "msppt8.h" //msppt9.h for PowerPoint 2000, msppt.h for PowerPoint 2002 and PowerPoint 2003 // Ole initialization class. class OleInitClass { public: OleInitClass() { OleInitialize(NULL); } ~OleInitClass() { OleUninitialize(); } }; // This global class calls OleInitialize() at // application startup, and calls OleUninitialize() // at application exit. OleInitClass g_OleInitClass;
- 编译并运行。