介绍
这期内容当中小编将会给大家带来有关怎么在c++中使用COM接口操作PPT,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
<强> 1.1进入类向导强>
<强> 1.2添加PowerPoint COM接口强>
<强> 1.3添加Excel COM接口强>
选中所有的COM接口,生成接口文件。
<强>三、定义PPT文件基础操作函数(头文件略)
强>
<强> 3.1定义PPT应用基础对象
强>
class CPPTObject { 公众: CApplication 才能;m_PPTApp; CSlides 才能;m_Slides; CSlide 才能;m_curSlide; CPresentation 才能;m_Presentaion; };
<强> 3.2启动PowerPoint软件,调用COM接口需要安装办公室
强>
//,创建PPT应用,启动PowerPoint程序。 bool CPPTUtil:: CreatePPTApplication () { COleException 才能;异常; LPCSTR 才能;str =,“Powerpoint.Application"; 如果才能(! m_pPPTObject→m_PPTApp.CreateDispatch (str,和例外)) {才能 ,,,AfxMessageBox (MB_SETFOREGROUND exception.m_sc也); ,,,return 假; ,,} m_pPPTObject才能→m_PPTApp.put_Visible(真正的); return 才能;真实; }
<强> 3.3打开PPT模板文件。修改PPT内容前,先打开PPT。
强>
//,打开模板PPT。 bool CPPTUtil:: OpenPPT (const std:: string&, pptPath) { CPresentations 才能;presentations =, m_pPPTObject→m_PPTApp.get_Presentations (); m_pPPTObject才能→m_Presentaion =, presentations.Open(装运箱(pptPath.c_str ()),, 0, 0, 1); m_pPPTObject→才能m_Slides =, m_pPPTObject→m_Presentaion.get_Slides (); return 才能;真实; }
3.4保存PPT文件内容,关闭文件,退出PowerPoint程序。
//,关闭PPT,保存数据关闭。 bool CPPTUtil:: ClosePPT () { m_pPPTObject→才能m_Presentaion.Save (); m_pPPTObject→才能m_Presentaion.Close (); m_pPPTObject→才能m_PPTApp.Quit (); return 才能;真实; }
<强> 3.5选中具体的PPT幻灯片。
强>
//,选中PPT指定索引的幻灯片。 bool CPPTUtil:: SelectSlide (long slideIndex) { if 才能;(slideIndex 祝辞,m_pPPTObject→m_Slides.get_Count ()) {才能 ,,,return 假; ,,} m_pPPTObject→才能m_curSlide =, m_pPPTObject→m_Slides.Range (COleVariant (slideIndex)); return 才能;真实; }
<强>四、修改文本编辑框函数强>
//,修改文本框 bool CPPTUtil:: ModifyTextBox (const std:: string&, boxName,, const std:: string&, strValue) { CShapes 才能;shapes =, m_pPPTObject→m_curSlide.get_Shapes (); ,,(long 小姐:=,1;,小姐:& lt;=, shapes.get_Count ();, + + i) {才能 ,,,CShape 形状(shapes.Item (COleVariant(我))); ,,,CString name =, shape.get_Name (); ,,,如果(shape.get_Type(),==,(长)办公室::msoTextBox ,,,,,,,,name.Compare(装运箱(boxName.c_str ())),==, 0), ,,,{ ,,,,,CTextFrame textFrame =, shape.get_TextFrame (); ,,,,,CTextRange textRange =, textFrame.get_TextRange (); ,,,,,CString txt =, textRange.get_Text (); ,,,,,textRange.put_Text (strValue.c_str ()); ,,,} ,,} return 才能;真实; }
boxName对应于PPT中形状的名字。这个形状名称貌似在PowerPoint中没有地方能看的到,也没有办法修改。只能在调试时记录下来。
<强>五、修改PPT中的图表函数。先在PPT中定义图表模板,通过COM接口修改图表数据。
强>
<强> 5.1定义图表数据结构。图表的数据都是用Excel存储的。
强>
5.1.1定义单元格数据结构
CCellDataCom:: CCellDataCom (const CellValueType  valueType,, const std:: string&, strValue,, ,,,,,const int iRow,, const int iCol) { 时间=m_ValueType 才能;valueType; 时间=m_strValue 才能;strValue; 时间=m_strPos 才能;indexToString (iCol iRow也); }//,获取单元格值类型 CellValueType CCellDataCom:: getValueType () { return 才能;m_ValueType; }//,获取字符串类型值 const std:: string&, CCellDataCom: getStringValue () { return 才能;m_strValue; }//,获取整型值 long CCellDataCom:: getLongValue () { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null怎么在c++中使用COM接口操作PPT