vc ppt

 VC6.0控制PPT很方便,网上的代码也很多,到了.NET,操作office的方式有所改进,完全按6.0的方式会出很多问题,根据做的有关用VS2008操作PPT的工作,整理一下。

开始是要用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(); //获得幻灯片总数
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用MFC 显示PPT //启动 PowerPoint: void CMainFrame::OnPowerpointStartpowerpoint() { /// Check if the IDispatch connection exists with PowerPoint, // if not create one. if (m_ppt.m_lpDispatch == NULL) { // Create IDispatch connection to PowerPoint. m_ppt.CreateDispatch("PowerPoint.Application"); }; // Bring the PowerPoint application to the front. m_ppt.Activate(); } void CMainFrame::OnPowerpointStartslideshow() { _Presentation oPresentation; SlideShowSettings oShow; // Attach to the Active Presentation. oPresentation.AttachDispatch(m_ppt.GetActivePresentation()); // Attach to the slide-show settings. oShow.AttachDispatch(oPresentation.GetSlideShowSettings()); // Run the slide show. oShow.Run(); } // 创建幻灯片: void CMainFrame::OnPowerpointCreateslide() { // Connect to the active presentation. There is no error trapping. // If the active presentation the framework traps // the error and displays a message box. _Presentation ActivePresentation(m_ppt.GetActivePresentation()); // Connect to the slides collection. Slides oSlides(ActivePresentation.GetSlides()); // This constant is defined in the PowerPoint Object model. // You can use the Object Browser, with Visual Basic Editor // (VBE), to look up the different constant values. const ppLayoutTitleOnly = 11; // Add a new slide to the presentation. This code adds the new // slide to the end of the presentation. oSlides.Add(oSlides.GetCount() + 1l, ppLayoutTitleOnly); } // 创建演示文稿: void CMainFrame::OnPowerpointCreatepresentation() { Presentations PresCollection; // Make sure there is a dispatch pointer for PowerPoint. if(m_ppt.m_lpDispatch == NULL) { // Display a message indicating that PowerPoint is not running. MessageBox("PowerPoint is not running.", "Start PowerPoint"); } else { // Bring PowerPoint to the front. m_ppt.Activate(); // Attach the presentations collection to the PresCollection // variable. PresCollection.AttachDispatch(m_ppt.GetPresentations()); // Create a new presentation. PresCollection.Add(1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值