C ++ uses the COM interface to operate the PPT

Source: Internet
Author: User

C ++ uses the COM interface to operate the PPT
I. background description

In the VS environment, you can develop C ++ code to operate the PPT and modify the PPT template. Including modifying text labels, charts, and tables. To meet the requirements of most software to generate a PPT report, manually create a PPT template and modify the template data in the program.

2. Development Environment Construction

Use the Class Wizard in VS2012 to create the COM interface for PowerPoint and Excel. Because you need to operate the charts in the PPT, the chart data is stored in Excel, to modify the chart data, you must generate the Excel COM interface.

1.1 enter the Class Wizard

1.2 Add the PowerPoint COM interface

1.3 add an Excel COM interface


Select All COM interfaces to generate interface files. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoMiBpZD0 = "3-defining pptfile basic operation function header file omitted"> 3. Defining pptfile basic operation function (header file omitted) 3.1 defining basic PPT application objects

class CPPTObject{public:    CApplication m_PPTApp;    CSlides m_Slides;    CSlide m_curSlide;    CPresentation m_Presentaion;};
3.2 start the PowerPoint software. To call the COM interface, you must install the Office
// Create a PPT application and start the powerpoint program. Bool CPPTUtil: CreatePPTApplication () {COleException exception; LPCSTR str = "Powerpoint. Application"; if (! M_pPPTObject-> m_PPTApp.CreateDispatch (str, & exception) {AfxMessageBox (exception. m_ SC, MB_SETFOREGROUND); return false;} m_pPPTObject-> destroy (true); return true ;}
3.3 open the PPT template file. Open the PPT before modifying the content.
// Open the template ppt. Bool CPPTUtil: OpenPPT (const std: string & pptPath) {CPresentations presentations = m_pPPTObject-> m_PPTApp.get_Presentations (); m_pPPTObject-> m_Presentaion = presentations. open (CString (pptPath. c_str (), 0, 0, 1); m_pPPTObject-> m_Slides = m_pPPTObject-> m_Presentaion.get_Slides (); return true ;}
3.4 Save the PPT file content, close the file, and exit the PowerPoint program.
// Close the PPT and save the data. Bool CPPTUtil: ClosePPT () {m_pPPTObject-> m_Presentaion.Save (); m_pPPTObject-> m_Presentaion.Close (); m_pPPTObject-> m_PPTApp.Quit (); return true ;}
3.5 select a specific PPT slide.
// Select the slide of the index specified by the PPT. Bool CPPTUtil: SelectSlide (long slideIndex) {if (slideIndex> m_pPPTObject-> m_Slides.get_Count () {return false ;} m_pPPTObject-> m_curSlide = m_pPPTObject-> m_Slides.Range (COleVariant (slideIndex); return true ;}
4. Modify the text edit box function
// Modify the textbox bool CPPTUtil: ModifyTextBox (const std: string & boxName, const std: string & strValue) {CShapes shapes = m_pPPTObject-> m_curSlide.get_Shapes (); for (long I = 1; I <= shapes. get_Count (); ++ I) {CShape shape (shapes. item (COleVariant (I); CString name = shape. get_Name (); if (shape. get_Type () = (long) Office: msoTextBox & name. compare (CString (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 true ;}

BoxName corresponds to the Shape Name in the PPT. This Shape Name seems to be invisible in PowerPoint and cannot be modified. It can only be recorded during debugging.

5. Modify the chart functions in the PPT. First, define the chart template in the PPT and modify the chart data through the COM interface. 5.1 define the chart data structure. Charts are stored in Excel.

5.1.1 define the cell Data Structure

CCellDataCom: CCellDataCom (const CellValueType valueType, const std: string & strValue, const int iRow, const int iCol) {m_ValueType = valueType; m_strValue = strValue; m_strPos = indexToString (iRow, iCol);} // obtain the cell value type CellValueType CCellDataCom: getValueType () {return m_ValueType;} // obtain the string type value const std: string & CCellDataCom: getStringValue () {return m_strValue;} // obtain the integer value long CCellDataCom: getLongValue () {return atol (m_strValue.c_str ();} // obtain the floating point value double CCellDataCom: getDoubleValue () {return atof (m_strValue.c_str ();} // obtain the cell location name const std: string & CCellDataCom: getPos () {return m_strPos ;} // convert the cell coordinate conversion name string CString CCellDataCom: indexToString (int row, int col) {CString strResult; if (col> 26) {strResult. format (_ T ("% c % d"), 'A' + (col-1)/26-1, 'A' + (col-1) % 26, row);} else {strResult. format (_ T ("% c % d"), 'A' + (col-1) % 26, row);} return strResult ;} 5.1.2 define the chart data structure // insert a line of record void CChartDataCom: insertRowData (const std: list
  
   
& LstRowData) {m_lstValue.push_back (lstRowData) ;}// obtain the chart data const std: list
   
    
> & CChartDataCom: getValue () const {return m_lstValue ;}
   
  
5.2 modify chart data Functions

// Modify the chart

Bool CPPTUtil: ModifyChart (const std: string & chartName, const CChartDataCom & chartData) {CShapes shapes = m_pPPTObject-> m_curSlide.get_Shapes (); for (long I = 1; I <= shapes. get_Count (); ++ I) {CShape shape (shapes. item (COleVariant (I); if (shape. get_Type ()! = (Long) Office: msoChart | chartName! = Std: string (shape. get_Name (). getBuffer () {continue;} // modify the chart data return ModifyChartData (shape. get_Chart (), chartData);} return false;} // modify the chart data bool CPPTUtil: ModifyChartData (CChart chart, const CChartDataCom & chartData) {// activate the excel data table of the chart component and open the embedded excel file. CChartData chartDataModel = chart. get_ChartData (); chartDataModel. activate (); CWorkbook workBook = chartDataModel. get_Workbook (); CWorksheets s Heets = workBook. get_Worksheets (); if (sheets. get_Count () = 0) {return false;} // obtain the first sheet. The data of the chart component is on the first sheet page embedded in excel. VARIANT vaSheetIndex; vaSheetIndex. vt = VT_I4; vaSheetIndex. lVal = 1; CWorksheet sheet = sheets. get_Item (vaSheetIndex); bool bRet = true; // cyclically modify the cell data const std: list
  
   
> & Amp; lstValue = chartData. getValue (); std: list
   
    
>:: Const_iterator iterAllData = lstValue. begin (); for (; iterAllData! = LstValue. end (); ++ iterAllData) {std: list
    
     
: Const_iterator iterRowData = iterAllData-> begin (); for (; iterRowData! = IterAllData-> end (); ++ iterRowData) {bRet = ModifyCellData (sheet, * iterRowData); if (bRet = false) {break ;}} if (bRet = false) {break ;}// close Excel CApplication0 app0 = workBook. get_Application (); app0.Quit (); Sleep (2000); return bRet;} // modify the cell data bool CPPTUtil: ModifyCellData (CWorksheet sheet, CCellDataCom cellData) {const std :: string & cellPos = cellData. getPos (); CRange range = sheet. get_Range (COleVariant (cellPos. c_str (), COleVariant (cellPos. c_str (); COleVariant * pOleVar = NULL; if (cellData. getValueType () = CELL_STRING_TYPE) {pOleVar = new COleVariant (CString (cellData. getStringValue (). c_str ();} else if (cellData. getValueType () = CELL_LONG_TYPE) {pOleVar = new COleVariant (cellData. getLongValue ();} else if (cellData. getValueType () = CELL_DOUBLE_TYPE) {pOleVar = new COleVariant (cellData. getDoubleValue ();} else {return false;} range. put_Value2 (* pOleVar); delete pOleVar; return true ;}
    
   
  
6. Merge multiple pptfile Functions
// Merge PPTbool CPPTUtil: MergePPT (const std: string & outputPPTPath, const std: list
  
   
& LstMergePPTPath) {CApplication pptApp; COleException exception; // open the PowerPoint program LPCSTR str = "Powerpoint. Application"; if (! PptApp. createDispatch (str, & exception) {AfxMessageBox (exception. m_ SC, MB_SETFOREGROUND); return false;} pptApp. put_Visible (true); // open the output file CPresentations presentations = pptApp. get_Presentations (); CPresentation outPresention = presentations. open (CString (outputPPTPath. c_str (), 0, 0, 1); // cyclically open the merged file and insert the PPT page std: list
   
    
: Const_iterator iterMergeFile = lstMergePPTPath. begin (); for (; iterMergeFile! = LstMergePPTPath. end (); ++ iterMergeFile) {CPresentation mergePresention = presentations. open (CString (iterMergeFile-> c_str (), 0, 0, 1); CSlides mergeSlides = mergePresention. get_Slides (); int pageNum = mergeSlides. get_Count (); mergePresention. close (); // merge the PPT tab CSlides outSlides = outPresention. get_Slides (); outSlides. insertFromFile (CString (iterMergeFile-> c_str (), outSlides. get_Count (), 1, pageNum);} outPresention. save (); outPresention. close (); pptApp. quit (); return true ;}
   
  

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.