自己动手:VC 操作PowerPoint

     前段时间突然接一小项目,其实也就是一个实例罢了,要求将程序中的位图按照一定的格式写入PowerPoint当中,经过自己的一番推敲终于搞定今天将具体的操作记录在此,方便今后改进。

   环境及软件配置:

        (1)PowerPoint2003

        (2)VC 6.0.

 操作步骤:

        首先新建一简单的MFC 对话框应用程序,在其中增加一按钮方便我们测试,双击按钮添加按钮处理事件。

        打开类向导对话框;AddClass->from a library->切换至PowerPoint安装目录寻找:MSPPT.OLB文件,由于我是默认安装所以在目录:C:\Program Files\Microsoft Office\OFFICE11 中 选中此文件后打开confirm Classes对话框 按住键盘ctrl依次选择:Application.SlideShowWindow,Selection,Fonts,Slides,SlideRange,RGBColor等我们需要操作的类,  然后给文件命名即可。

      在button事件所在文件中包含新增加的文件。

      然后写入如下代码:

void CTestpptDlg::OnButton1()
{
   // Commonly used OLE-variants.
   COleVariant
               covTrue((short)TRUE),
               covFalse((short)FALSE),
               covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

   _Application   app;
   Presentations  presentations;
   _Presentation  presentation;
   Slides         slides;
   _Slide         slide;
   ShapeRange     shaperange;
   Shapes         shapes;
   Shape          shape;
   TextFrame      textframe;

   TextRange      textrange;
   Font           font;
   FillFormat     fillformat;
   ColorFormat    colorformat;
   ShadowFormat   shadow;

      CString strPic1 ="C:\\Program Files\\Microsoft Office\\Templates\\Presentation Designs\\angrybg.bmp";
      CString strTemplate =
         "C:\\Program Files\\Microsoft Office\\Templates\\Presentation Designs\\Zesty.pot";


    if(!app.CreateDispatch("Powerpoint.Application"))
      {
         AfxMessageBox("Could not create Powerpoint object.");
         return;
      }

      //Make the application visible and minimized.
      app.SetVisible((long)TRUE);
      app.SetWindowState((long) 2);   //ppWindowMinimized=2

   presentations=app.GetPresentations();
      presentation=presentations.Add(TRUE);

  


      slides = presentation.GetSlides();
      PageSetup page = presentation.GetPageSetup();
      float  hight = page.GetSlideHeight();
   float width = page.GetSlideWidth();//Add the first slide to the presentation.
      slide = (slides.Add((long)1, (long)1));   //pptLayoutTitleOnly=11
      shapes=slide.GetShapes();
   int m =shapes.GetCount();

   while (m != 0)
  {
   shape = shapes.Item(COleVariant((short)1));
   shape.Delete();
   m--;
  }
     
   float w =width/45;
   float h = hight/33;
   float cell =hight/33;
      shapes = slide.GetShapes();

InsertLabel(shapes,"图片1:北京",cell*4-10,cell*4,cell*9,cell*4,RGB(0,255,255),20,"Comic Sans MS");
shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell*4,(float)cell*6,(float)cell*9,(float)cell*10);

InsertLabel(shapes,"图片2:上海",22*cell-10,cell*4,cell*14,cell*4,RGB(255,0,0),20,"Comic Sans MS");
shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell*22,(float)cell*6,cell*14,(float)cell*10);
   

      slide = (slides.Add((long)2, (long)1));   //pptLayoutTitleOnly=11
      shapes = slide.GetShapes();
   m =shapes.GetCount();
  
   while (m != 0)
   {
    shape = shapes.Item(COleVariant((short)1));
    shape.Delete();
    m--;
  }

  
   InsertLabel(shapes,"图片1:北京",cell-10,cell,cell*9,cell*4,RGB(0,255,255),20,"Comic Sans MS");
       shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell,(float)cell*3,(float)cell*9,(float)cell*10);

    InsertLabel(shapes,"图片1:北京",cell-10,cell*19,cell*9,cell*4,RGB(0,255,255),20,"Comic Sans MS");
       shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell,(float)cell*21,(float)cell*9,(float)cell*10);


 
    InsertLabel(shapes,"图片1:北京",cell*12-10,cell,cell*17,cell*4,RGB(0,255,255),20,"Comic Sans MS");
       shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell*12,(float)cell*3,(float)cell*17,(float)cell*4);

  
    InsertLabel(shapes,"图片2:上海",cell*14-10,cell*9,cell*14,cell*4,RGB(0,0,255),20,"Comic Sans MS");
       shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell*14,(float)cell*11,(float)cell*14,(float)cell*12);


    InsertLabel(shapes,"图片1:北京",cell*12-10,cell*25,cell*17,cell*4,RGB(255,0,255),20,"Comic Sans MS");
       shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell*12,(float)cell*27,(float)cell*17,(float)cell*4);

 
    InsertLabel(shapes,"图片1:北京",cell*33-10,cell,cell*9,cell*4,RGB(255,0,255),20,"Comic Sans MS");
       shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell*33,(float)cell*3,(float)cell*9,(float)cell*10);
 

   
    InsertLabel(shapes,"图片1:北京",cell*33-10,cell*19,cell*9,cell*4,RGB(255,0,255),20,"Comic Sans MS");
       shapes.AddPicture(strPic1,(long)0,(long)-1,(float)cell*33,(float)cell*21,(float)cell*9,(float)cell*10);
 

       SlideShowTransition   show;
      SlideShowSettings   slideshow;

      //Set the Transition effects for Slide 1.
      slide = slides.Item(COleVariant((short)1));
      show = slide.GetSlideShowTransition();
      show.SetEntryEffect((long)769);      //ppEffectBlindsHorizontal=769
      show.SetAdvanceOnTime((long)-1);
      show.SetAdvanceTime((float)3);

      //Set the Transition effects for Slide 2.
      slide = slides.Item(COleVariant((short)2));
      show = slide.GetSlideShowTransition();
      show.SetEntryEffect((long)770);      //ppEffectBlindsVertical=770
      show.SetAdvanceOnTime((long)-1);
      show.SetAdvanceTime((float)3);


      //Run the show.
      slideshow = presentation.GetSlideShowSettings();
      slideshow.Run();

      //Quit the application after waiting 9 seconds (the length of the
      //slide show).
      ::Sleep(1000);
      presentation.SetSaved((long)-1);
      app.Quit();

}
void CTestpptDlg::InsertLabel(Shapes &shapes,CString tittle,float left,float top,float width,float hight,long cols,float size,CString fontstyle )
{
 Shape shape= shapes.AddLabel((long)1,left,top,width,hight);
TextFrame textframe = shape.GetTextFrame();
TextRange textrange = textframe.GetTextRange();
 textrange.SetText(tittle);//Set the text
 
Font font = textrange.GetFont();
font.SetName(fontstyle);              //Set the font name.
font.SetSize(size);
ColorFormat col= font.GetColor();
col.SetRgb(cols);
}

以上代码共生成两张ppt

                                 1                                                                                                                         2

自己动手:VC 操作PowerPoint_第1张图片自己动手:VC 操作PowerPoint_第2张图片

 

你可能感兴趣的:(自己动手:VC 操作PowerPoint)