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