Search for text in a PowerPoint document using C #

Source: Internet
Author: User
Tags foreach object model

It's easy to programmatically search for text in Word, Excel documents, and in PowerPoint, using the object model to help us understand the document structure of office.

Search ideas and methods are basically the same, using the PowerPoint Application object to open the specified document, the document object to get the document, and then use the appropriate object to split the document into a search scope of the object to search.

Opening the VBA Help document for PowerPoint Vbapp10.chm, it's easy to find the few collections and objects we need, based on the object Model diagram: application, presentations, presentation, Slides, Slide, TextFrame, TextRange. Where presentation represents a PowerPoint document, slide represents a single slide in a PowerPoint document, TextFrame is a text box on a slide, and TextRange is text in a text box.

To open a PowerPoint document:

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);

The third parameter of the Open () method is described in the Help document as follows:

Untitled optional. MsoTriState type. Specifies whether the file has a title.

Because it's untitled, you can follow the code above to open the document to refer to the title of the PowerPoint document, and if you don't want to use the caption, change the enumeration msofalse to msotrue.

Search text:

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;
       }
     }
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.