用編程的方式根據對象模型很容易實現在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;
}
}
}
}
新聞熱點
疑難解答