日期:2014-05-17 浏览次数:21358 次
//实现PPT文本查找功能
//关键代码
private void searchPPT(string[] keyWordList,string pptFileName)//在指定的ppt文档中搜索keyWord
         {
              //Microsoft powerpoint 11.0 object library这个是所添加的引用
             
             
               showMsg(lbShowMsg,"启动搜索"+pptFileName);
              //其中Presentation代表一个 PowerPoint 文档,Slide表示PowerPoint文档中的单张幻灯片
              //TextFrame是幻灯片上的文本框,TextRange是文本框中的文本。
              PowerPoint.ApplicationClass pa;
              PowerPoint.Presentation pp;
              pa=null;
              pp=null;
              try
              {
                  
                   showMsg(lbShowMsg,"尝试打开 "+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)
                   {
                      
              showMsg(lbShowMsg,"正在搜索"+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;
                   }
             
                  
              }
         }