加载中…
个人资料
春山飞雪
春山飞雪
  • 博客等级:
  • 博客积分:0
  • 博客访问:423,358
  • 关注人气:352
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

C#实现PPT文本查找功能

(2010-01-24 11:18:31)
标签:

c

ppt

powerpoint

查找

it

分类: C/C /C#编程

//实现PPT文本查找功能关键代码

//添加引用 Microsoft powerpoint 11.0 object library

//在指定的ppt文档中搜索keyWord

private void searchPPT(string[] keyWordList,string pptFileName)

{

       MessageBox.Show( "启动搜索" + pptFileName);

      //其中Presentation代表一个 PowerPoint 文档,Slide表示PowerPoint文档中的单张幻灯片

      //TextFrame是幻灯片上的文本框,TextRange是文本框中的文本。

      PowerPoint.ApplicationClass pa;

      PowerPoint.Presentation pp;

      pa=null;

      pp=null;

      try

      { 

 

                 MessageBox.Show( "尝试打开 " + pptFileName);  

                   //打开ppt文档

                   pa=new PowerPoint.ApplicationClass();

                   pp=pa.Presentations.Open(pptFileName,

                       Microsoft.Office.Core.MsoTriState.msoTrue,

                       Microsoft.Office.Core.MsoTriState.msoTrue,Microsoft.Office.Core.MsoTriState.msoFalse);

 

                   PowerPoint.TextRange oText;    

                   int slideCount=pp.Slides.Count;//总的幻灯片数

        

                   foreach(PowerPoint.Slide slide in pp.Slides)//对每张幻灯片

                   {

                      

                       MessageBox.Show("正在搜索" + pptFileName + " 幻灯片" + slide.SlideNumber.ToString() + "/" + slideCount);

                       foreach(PowerPoint.Shape shape in slide.Shapes)//对所有的元素

                       {

                      

                     if(shape.HasTextFrame==Microsoft.Office.Core.MsoTriState.msoTrue)//如果此幻灯片中有文本框

                            {

                                 foreach(string keyWord in keyWordList)//对每组关键字

                                 {

                                     oText=null;

                                     oText=shape.TextFrame.TextRange.Find

 

                      (keyWord,0,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue);

                                     if (oText!=null)

                                     {

                                    

                                          string temp=pptFileName.Remove(0,pptFileName.LastIndexOf("\\")+1);

                                          string name=temp.Remove(temp.LastIndexOf("."),4);

                                          int index=slide.SlideNumber;

                                          lbResult.Items.Add(name+" 幻灯片"+index.ToString()+"/"+slideCount);//添加到搜索结果中

                                          lbResult.Update();

                                          resultText.Add(shape.TextFrame.TextRange.Text);

                                          continue;

                                     }

                                 }

                            }

                       }

                   }

             

              }

        

              catch

              {

              }

              finally

              {

               //释放资源

                   if(pp!=null)

                   {

                       System.Runtime.InteropServices.Marshal.ReleaseComObject(pp);

                       pp=null;

                   }

                   if(pa!=null)

                   {

                       System.Runtime.InteropServices.Marshal.ReleaseComObject(pa);

                       pa=null;

                   }

             

                  

              }

   }

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有