用C#实现在PowerPoint文档中搜索文本
搜索的思路和方法基本是一樣的,用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;
????????????}
????????}
????}
}
?
總結
以上是生活随笔為你收集整理的用C#实现在PowerPoint文档中搜索文本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Excel、Exchange和C#
- 下一篇: C#如何在Form中嵌入并且操作Exce