VS2008操作PPT
来源:互联网 发布:caffe的安装和配置 编辑:程序博客网 时间:2024/05/16 11:12
VC6.0控制PPT很方便,网上的代码也很多,到了.NET,操作office的方式有所改进,完全按6.0的方式会出很多问题,根据做的有关用VS2008操作PPT的工作,整理一下。开始是要用VS2005的,发现问题很多,似乎根本都调不了,改到2008后很快就能运行了,按理说两个应该是差不多的,不知道2005出什么问题了。现在用2008操作PPT:
然后添加PPT的类库(word,excel同理),如下
选择如下
选择ppt类库
添加接口(如果不知道用哪个,那就都添加)
确定,将添加很多头文件到工程,如下
要使用哪个类,可以把相应的头文件包含到stdafx.h文件中,注意要添加如下的代码,好像是兼容的问题
上面的路径随各人机子而已。上面的代码可以加到stdafx.h的最后,然后在其后面添加要用的类的头文件,如
以上类是比较常用的,前3个一般必须要,后面的根据自己的使用情况。
另外还要把以上添加的每个头文件的import注释掉,如CApplication.h
// Machine generated IDispatch wrapper class(es) created with Add Class from Typelib Wizard
//#import "D:\\Program Files\\Microsoft Office\\OFFICE11\\MSPPT.OLB" no_namespace
// CApplication wrapper class
一般如果编译出现好多错,就看看是否是哪个头文件的import没有注释掉,这个很容易忘掉。
方便起见,给dlg类添加如下成员变量和函数
添加如下按钮的实现代码
提示:由于没有帮助文件,所以如果有想要完成的功能,可以用下面的方法
1,录制VBA宏,根据录制的宏代码改编c++代码
首先创建一个MFC的对话框程序,如图
然后添加PPT的类库(word,excel同理),如下
选择如下
选择ppt类库
添加接口(如果不知道用哪个,那就都添加)
确定,将添加很多头文件到工程,如下
要使用哪个类,可以把相应的头文件包含到stdafx.h文件中,注意要添加如下的代码,好像是兼容的问题
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\mso.dll" \ rename_namespace("Office"), named_guids, exclude("Pages")using namespace Office;#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.olb" \ rename_namespace("VBE6")using namespace VBE6;
上面的路径随各人机子而已。上面的代码可以加到stdafx.h的最后,然后在其后面添加要用的类的头文件,如
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\mso.dll" \ rename_namespace("Office"), named_guids, exclude("Pages")using namespace Office;#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.olb" \ rename_namespace("VBE6")using namespace VBE6;#include "CApplication.h"#include "CPresentations.h"#include "CPresentation.h"#include "CSlideShowView.h"#include "CSlideShowWindows.h"#include "CSlideShowWindow.h"#include "CSlideShowSettings.h"#include "CSlides.h"#include "CSlide.h"#include "CDocumentWindow.h"#include "CShapes.h"#include "CShape.h"#include "CTextFrame.h"#include "CTextRange.h"#include "CView0.h"#include "CTable0.h"#include "CShapeRange.h"#include "CCellRange.h"#include "CCell.h"#include "CSelection.h"#include "CRows.h"#include "CRow.h"
以上类是比较常用的,前3个一般必须要,后面的根据自己的使用情况。
另外还要把以上添加的每个头文件的import注释掉,如CApplication.h
// Machine generated IDispatch wrapper class(es) created with Add Class from Typelib Wizard
//#import "D:\\Program Files\\Microsoft Office\\OFFICE11\\MSPPT.OLB" no_namespace
// CApplication wrapper class
一般如果编译出现好多错,就看看是否是哪个头文件的import没有注释掉,这个很容易忘掉。
方便起见,给dlg类添加如下成员变量和函数
public:CApplication app;CPresentations Presentations;CPresentation Presentation;CSlideShowView View;CSlideShowWindows SlideShowWindows;CSlideShowWindow SlideShowWindow;CSlideShowSettings slideshow;CSlides slides;CSlide slide;CDocumentWindow documentwindow;CView0 view0;CShapeRange ShapeRange;CCellRange CellRange;CCell Cell1;CSelection Selection; BOOL InsertText(CString str, int pageNum, int shapeNum);BOOL InsertForm(CString str, int pageNum, int shapeNum, int rownum, int colomnnum);BOOL InsertPicture(LPTSTR path, int pageNum, int shapeNum);BOOL InsertObject(LPTSTR path, int pageNum, int shapeNum);
添加如下按钮的实现代码
//打开ppt应用程序void CVS08PPTForBlogDlg::OnBnClickedButtonStart(){ // TODO: Add your control notification handler code here if(!app.CreateDispatch("Powerpoint.Application")) { AfxMessageBox("Couldn't start PowerPoint."); } else // Make PowerPoint visible and display a message { app.put_Visible(TRUE); TRACE("PowerPoint is Running!"); }}//打开pptvoid CVS08PPTForBlogDlg::OnBnClickedButtonOpen(){ // TODO: Add your control notification handler code here 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.get_Presentations(); Presentation = Presentations.Open(strFileName,0,0,1); }}//关闭pptvoid CVS08PPTForBlogDlg::OnBnClickedButtonClose(){ // TODO: Add your control notification handler code here //documentwindow=app.get_ActiveWindow();//获得活动的文档 //documentwindow.Close();//关闭当前活动的文档 app.Quit();}//开始放映void CVS08PPTForBlogDlg::OnBnClickedButtonRun(){ // TODO: Add your control notification handler code here Presentation = app.get_ActivePresentation(); slides = Presentation.get_Slides(); // Show the first slide of the presentation slide = slides.Item(COleVariant((long)1)); //Run the show slideshow = Presentation.get_SlideShowSettings(); slideshow.Run();}//下一张放映的pptvoid CVS08PPTForBlogDlg::OnBnClickedButtonPrevious(){ // TODO: Add your control notification handler code here Presentation = app.get_ActivePresentation(); SlideShowWindow = Presentation.get_SlideShowWindow(); View = SlideShowWindow.get_View(); View.Previous();}//上一张放映的pptvoid CVS08PPTForBlogDlg::OnBnClickedButtonNext(){ // TODO: Add your control notification handler code here Presentation = app.get_ActivePresentation(); SlideShowWindow = Presentation.get_SlideShowWindow(); View = SlideShowWindow.get_View(); View.Next();}void CVS08PPTForBlogDlg::OnBnClickedButtonInserttext(){ // TODO: Add your control notification handler code here InsertText(CString("测试1"),1,1);}void CVS08PPTForBlogDlg::OnBnClickedButtonInsertform(){ // TODO: Add your control notification handler code here InsertForm(CString("测试1"),2,1,1,1);}void CVS08PPTForBlogDlg::OnBnClickedButtonInsertpicture(){ // TODO: Add your control notification handler code here //添加图片 InsertPicture("d:\\resource\\test.jpg", 2, 2);}void CVS08PPTForBlogDlg::OnBnClickedButtonInsertobject(){ // TODO: Add your control notification handler code here //添加视频 InsertObject("d:\\resource\\rbf.wmv",3,1);}//在第pageNum张ppt的第shapeNum个shape插入文字str(shapeNum为按下tab时的顺序号)BOOL CVS08PPTForBlogDlg::InsertText(CString str, int pageNum, int shapeNum){ CShapes m_Shapes; CShape m_Shape; Presentation = app.get_ActivePresentation(); slides = Presentation.get_Slides(); slide = slides.Item(COleVariant((long)pageNum)); m_Shapes = slide.get_Shapes(); m_Shape = m_Shapes.Item(COleVariant((long)shapeNum)); // 插入文字 CTextFrame m_TextFrame; CTextRange m_TextRange; m_TextFrame = m_Shape.get_TextFrame(); m_TextRange = m_TextFrame.get_TextRange(); m_TextRange.put_Text(str); return TRUE;}//在第pageNum张ppt的第shapeNum个shape表示的表格的rownum行colomnnum列插入文字strBOOL CVS08PPTForBlogDlg::InsertForm(CString str, int pageNum, int shapeNum, int rownum, int colomnnum){ CShapes m_Shapes; CShape m_Shape; CTable0 table; CTextFrame m_TextFrame; CTextRange m_TextRange; CCell Cell1; Presentation = app.get_ActivePresentation(); slides = Presentation.get_Slides(); slide = slides.Item(COleVariant((long)pageNum)); m_Shapes = slide.get_Shapes(); m_Shape = m_Shapes.Item(COleVariant((long)shapeNum)); table = m_Shape.get_Table(); Cell1 = table.Cell(rownum,colomnnum); m_Shape = Cell1.get_Shape(); m_TextFrame = m_Shape.get_TextFrame(); m_TextRange = m_TextFrame.get_TextRange(); m_TextRange.put_Text(str); return TRUE;} BOOL CVS08PPTForBlogDlg::InsertPicture(LPTSTR path, int pageNum, int shapeNum){ CShapes m_Shapes; CShape m_Shape; Presentation = app.get_ActivePresentation(); slides = Presentation.get_Slides(); slide = slides.Item(COleVariant((long)pageNum)); m_Shapes = slide.get_Shapes(); m_Shape = m_Shapes.Item(COleVariant((long)shapeNum)); // 插入图片 float fleft = m_Shape.get_Left(); float ftop = m_Shape.get_Top(); float fwidth = m_Shape.get_Width(); float fheight = m_Shape.get_Height(); m_Shapes.AddPicture(path, FALSE, TRUE, fleft, ftop, fwidth, fheight); return TRUE;}BOOL CVS08PPTForBlogDlg::InsertObject(LPTSTR path, int pageNum, int shapeNum){ CShapes m_Shapes; CShape m_Shape; Presentation = app.get_ActivePresentation(); slides = Presentation.get_Slides(); slide = slides.Item(COleVariant((long)pageNum)); m_Shapes = slide.get_Shapes(); m_Shape = m_Shapes.Item(COleVariant((long)shapeNum)); // 插入对象 float fleft = m_Shape.get_Left(); float ftop = m_Shape.get_Top(); float fwidth = m_Shape.get_Width(); float fheight = m_Shape.get_Height(); m_Shapes.AddOLEObject(fleft, ftop, fwidth, fheight,NULL,path,0,"",0,"",0); return TRUE;}以上代码是假设ppt上已经有很多shape,如果是想往ppt上添加各种shape,可以调用shapes对象的Add..函数来实现,如添加表格 CShapes m_Shapes; CShape m_Shape; CTable0 table; CTextFrame m_TextFrame; CTextRange m_TextRange; CCell Cell1; CRows rows; Presentation = app.get_ActivePresentation(); slides = Presentation.get_Slides(); slide = slides.Item(COleVariant((long)1)); m_Shapes = slide.get_Shapes(); m_Shape = m_Shapes.Item(COleVariant((long)17)); // 插入表格 float fleft = m_Shape.get_Left(); float ftop = m_Shape.get_Top(); float fwidth = m_Shape.get_Width(); float fheight = m_Shape.get_Height(); m_Shape = m_Shapes.AddTable(2,6,fleft, ftop, fwidth, fheight); // 插入2行6列的表格 table = m_Shape.get_Table(); rows = table.get_Rows(); rows.Add(1); // 第1行后插入一行 rows.Add(1); // 第1行后插入一行 rows.Add(2); // 第2行后插入一行 rows.Add(3); // 第3行后插入一行 return;
提示:由于没有帮助文件,所以如果有想要完成的功能,可以用下面的方法
1,录制VBA宏,根据录制的宏代码改编c++代码
2,猜函数,比如要插入文本,那就整个工程搜索put_Text,看哪个类有实现这个功能的函数,然后去凑那个函数。
备注:原文档中没有初始化的OLE的内容,经测试,发现在使用之前还需先初始化OLE,具体方式如下:
在应用程序的InitInstance()中初始化OLE,代码如下:
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
备注:原文档中没有初始化的OLE的内容,经测试,发现在使用之前还需先初始化OLE,具体方式如下:
在应用程序的InitInstance()中初始化OLE,代码如下:
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("Failed to initialize OLE");
return FALSE;
}
- VS2008操作PPT
- VS2008操作PPT
- POI操作PPT
- C#.net 操作 ppt
- Jacob操作PPT
- c# 操作PPT
- Jacob操作PPT
- VS2010操作PPT
- qt4.8.6操作ppt
- PPT基本操作
- HSLF操作PPT
- C#操作ppt,ppt转pdf
- 使用POI操作PPT文档
- java poi ppt操作示例
- poi操作PPT文档总结
- 使用POI操作PPT实例
- Java 利用POI操作PPT
- poi操作ppt创建表格
- Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中的路径问题,通配符映射 (
- mysql 一些经验
- javaScript学习随笔
- Linux 0.12 OS. math - compare.c
- [转]IO模式设置网络编程常见问题总结—IO模式设置,阻塞与非阻塞
- VS2008操作PPT
- CXF学习总结1 编写接口,实现类,Map自定义类型转换
- BackTrack5 (BT5) 无线wpa密码破解教程 gerix
- 网络数据请求方式(SOAP和JSON)
- OpenCV函数cvLogPolar对数极变换示例
- Minimum Path Sum
- 做任何事都需要坚持!
- 不使用 symbolicatecrash 解析崩溃堆栈
- HTML5 学习小结