开始是要用VS2005的,发现问题很多,似乎根本都调不了,改到2008后很快就能运行了,按理说两个应该是差不多的,不知道2005出什么问题了。现在用2008操作PPT:
首先创建一个MFC的对话框程序,如图
然后添加PPT的类库(word,excel同理),如下
添加接口(如果不知道用哪个,那就都添加)
确定,将添加很多头文件到工程,如下
要使用哪个类,可以把相应的头文件包含到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!");
}
}
//打开ppt
void 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);
}
}
//关闭ppt
void 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();
}
//下一张放映的ppt
void CVS08PPTForBlogDlg::OnBnClickedButtonPrevious()
{
// TODO: Add your control notification handler code here
Presentation = app.get_ActivePresentation();
SlideShowWindow = Presentation.get_SlideShowWindow();
View = SlideShowWindow.get_View();
View.Previous();
}
//上一张放映的ppt
void 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列插入文字str
BOOL 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,看哪个类有实现这个功能的函数,然后去凑那个函数。
11、在翻到首页按钮函数中添加如下代码:
void CXXXDlg::OnBtnFirst()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.First();
}
12、在翻到末叶按钮函数中添加如下代码:
void CXXXDlg::OnBtnLast()
{
Presentation = app.GetActivePresentation();
SlideShowWindow = Presentation.GetSlideShowWindow();
View = SlideShowWindow.GetView();
View.Last();
}
15.获得幻灯片总数
void CXXXDlg::OnBtnGetSlidesCount()
{
Presentations=app.GetActivePresentation();
slides=Presentation.GetSlides();
long endpos=slides.GetCount(); //获得幻灯片总数
}