位置: PPT中国·PPT资源网 >> 在线教程 >> PPT程序设计教程 >> 正文
用C#实现在PowerPoint文档中搜索文本[提供最好的PPT模板,打造最好的PPT门户]
用编程的方式根据对象模型很容易实现在Word、Excel文档中搜索文本,在PowerPoint里面也同样如此,使用对象模型有助于我们了解office的文档结构。

  搜索的思路和方法基本是一样的,用PowerPoint应用程序对象打开指定的文档,用文档对象获取文档,再使用合适的对象将文档分割成搜索范围适中的对象进行搜索。

  打开PowerPoint的VBA帮助文档VBAPP10.CHM,根据对象模型图,很容易找到我们需要的几个集合和对象:Application、Presentations、Presentation、Slides、Slide、TextFrame、TextRange。其中Presentation代表一个 PowerPoint 文档,Slide表示PowerPoint文档中的单张幻灯片,TextFrame是幻灯片上的文本框,TextRange是文本框中的文本。

  打开PowerPoint文档:

string filename="";

PowerPoint.Application pa=new PowerPoint.ApplicationClass();
PowerPoint.Presentation pp=pa.Presentations.Open(filename,
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                Microsoft.Office.Core.MsoTriState.msoFalse);
  Open()方法的第三个参数在帮助文档中的说明如下:

  Untitled     可选。MsoTriState 类型。指定文件是否有标题。

  因为是Untitled,所以按照上面的代码,打开文档之后才能引用PowerPoint文档的标题,如果不想使用标题,就要把枚举msoFalse改成msoTrue。
搜索文本:

string[] strKeyWordList={};    //要搜索的文本
PowerPoint.TextRange oText;
foreach(PowerPoint.Slide slide in pp.Slides)
{
    foreach(PowerPoint.Shape shape in slide.Shapes)
    {
        foreach(string strKeyWord in strKeyWordList)
        {
            oText=null;
            oText=shape.TextFrame.TextRange.Find(strKeyWord,0,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue);
            if (oText!=null)
            {
                MessageBox.Show("文档中包含指定的关键字 "+strKeyWord+" !","搜索结果",MessageBoxButtons.OK);
                continue;
            }
        }
    }
}用编程的方式根据对象模型很容易实现在Word、Excel文档中搜索文本,在PowerPoint里面也同样如此,使用对象模型有助于我们了解office的文档结构。

  搜索的思路和方法基本是一样的,用PowerPoint应用程序对象打开指定的文档,用文档对象获取文档,再使用合适的对象将文档分割成搜索范围适中的对象进行搜索。

  打开PowerPoint的VBA帮助文档VBAPP10.CHM,根据对象模型图,很容易找到我们需要的几个集合和对象:Application、Presentations、Presentation、Slides、Slide、TextFrame、TextRange。其中Presentation代表一个 PowerPoint 文档,Slide表示PowerPoint文档中的单张幻灯片,TextFrame是幻灯片上的文本框,TextRange是文本框中的文本。

  打开PowerPoint文档:

string filename="";

PowerPoint.Application pa=new PowerPoint.ApplicationClass();
PowerPoint.Presentation pp=pa.Presentations.Open(filename,
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                Microsoft.Office.Core.MsoTriState.msoFalse);
  Open()方法的第三个参数在帮助文档中的说明如下:

  Untitled     可选。MsoTriState 类型。指定文件是否有标题。

  因为是Untitled,所以按照上面的代码,打开文档之后才能引用PowerPoint文档的标题,如果不想使用标题,就要把枚举msoFalse改成msoTrue。
搜索文本:

string[] strKeyWordList={};    //要搜索的文本
PowerPoint.TextRange oText;
foreach(PowerPoint.Slide slide in pp.Slides)
{
    foreach(PowerPoint.Shape shape in slide.Shapes)
    {
        foreach(string strKeyWord in strKeyWordList)
        {
            oText=null;
            oText=shape.TextFrame.TextRange.Find(strKeyWord,0,Microsoft.Office.Core.MsoTriState.msoFalse,Microsoft.Office.Core.MsoTriState.msoTrue);
            if (oText!=null)
            {
                MessageBox.Show("文档中包含指定的关键字 "+strKeyWord+" !","搜索结果",MessageBoxButtons.OK);
                continue;
            }
        }
    }
}

发表评论】   【告诉好友】   【打印此文】  【收藏此页】  【关闭窗口】  【返回顶部
相关文章
最新推荐文章
最新热点文章
赞助商推荐
Copyright © 2006-2008 PPT中国·PPT资源网 All Rights Reserved
QQ群:PPT资源交流①群:679065(满) ②群:679075(欢迎加入) 网站备案号:粤ICP备06108872号