The introduction of Operation with VC for office is a lot, but never puts Word, Excel, and PowerPoint into a comprehensive introduction.
Due to the need for work, I need to operate Word, Excel, PowerPoint in my own software. So write your own experience and share it, I hope to help everyone. Of course, there are many cases, I hope everyone should point out.
Use an example to explain it, first create an MFC AppWizard (EXE) project, then select ClassWizard, select the Automation tab, select the AUTOFTLIBRARY, select from A Typelibrary, select the Microsoft Office 2000 Type List: Excel9.olb , MSPPT9.OLB, MSWORD9.OLB (in the Microsoft Office / Office Directory) Add all classes in the type library to your project.
Then write a class to manipulate offices!
Obtguigcomofficeprinter .h
#if! defined (AFX_OBTGUIGCOMOFFICEPRINTER_H__03A0C2D8_DFC8_4B51_8ADB_994B86BACB82__INCLUDED _) # define AFX_OBTGUIGCOMOFFICEPRINTER_H__03A0C2D8_DFC8_4B51_8ADB_994B86BACB82__INCLUDED_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
#include "comdef.h" #include "obtguigcommsword9.h" #include "obtguigcommsppt9.h" #include "obtguigcomexcel9.h"
Class AFX_EXT_CLASS OBTGUIGCOMOFFICEPRINTER {public: Obtguigcomofficeprinter (); virtual ~ Obtguigcomofficeprinter ();
// Operatorpublic: BOOL WordPrinterToJcf (LPCTSTR lpszFileName, LPCTSTR lpszActivePrinter); BOOL ExcelPrinterToJcf (LPCTSTR lpszFileName, LPCTSTR lpszActivePrinter); BOOL PowerPointPrinterToJcf (LPCTSTR lpszFileName, LPCTSTR lpszActivePrinter);
}
#include "stdafx.h" #include "ObtguigComofficeprinter.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
//// construction / destruction //
Obtguigcomofficeprinter :: Obtguigcomofficeprinter () {
}
Obtguigcomofficeprinter :: ~ Obtguigcomofficeprinter () {
}
BOOL ObtGuiGcomOfficePrinter :: PowerPointPrinterToJcf (LPCTSTR lpszFileName, LPCTSTR lpszActivePrinter) {_PptApplication m_powerpointApp; Presentations m_powerpointPres; _Presentation m_powerpointPre;
m_powerpointPres.ReleaseDispatch (); m_powerpointPre.ReleaseDispatch (); if (! m_powerpointApp.CreateDispatch ( "PowerPoint.Application")) {AfxMessageBox ( "Failed to create a PowerPoint Service!"); return FALSE;} m_powerpointApp.m_bAutoRelease = true; m_powerpointApp. SetVisible (TRUE); // must be set for PowerPoint TRUEm_powerpointPres.AttachDispatch (m_powerpointApp.GetPresentations ()); m_powerpointPres.Open (lpszFileName, -1, -1, -1); m_powerpointPre.AttachDispatch (m_powerpointApp.GetActivePresentation (), TRUE ); M_powerpointpre.printout (-1, -1, "", long (1), - 1); m_powerpointapp.quit ();
M_PowerPointPre.releaseDispatch (); m_powerpointpres.releaseDispatch (); m_powerpointapp.releaseDispatch ();
return TRUE;} BOOL ObtGuiGcomOfficePrinter :: ExcelPrinterToJcf (LPCTSTR lpszFileName, LPCTSTR lpszActivePrinter) {_ExcelApplication m_excelApp; // define application objects provided by Excel Workbooks m_excelBooks; _Workbook m_excelBook; m_excelBooks.ReleaseDispatch (); m_excelBook.ReleaseDispatch (); m_excelApp.m_bAutoRelease = True;
// Create an Excel 2000 server (started Excel) if (! M_excelapp.createdispatch ("excel.application")) {AFXMESSAGEBOX ("Create an Excel Service Failed!"); Return False;} m_excelapp.setVisible (false); // Setting // Create a new document to hide m_excelBooks.AttachDispatch (m_excelApp.GetWorkbooks (), true) using the template file; m_excelBook.AttachDispatch (m_excelBooks.Add (_variant_t (lpszFileName))); //m_excelApp.SetActivePrinter(lpszActivePrinter); // set current printer COleVariant covTrue ((short) TRUE), covFalse ((short) FALSE), covOptional ((long) DISP_E_PARAMNOTFOUND, VT_ERROR); // define printer and print m_excelBook.PrintOut (covOptional, covOptional, COleVariant (long (1)) , covFalse, covOptional, covOptional, covOptional, covOptional); m_excelApp.Quit (); // exit m_excelBook.ReleaseDispatch (); m_excelBooks.ReleaseDispatch (); m_excelApp.ReleaseDispatch (); return TRUE;}
BOOL ObtGuiGcomOfficePrinter :: WordPrinterToJcf (LPCTSTR lpszFileName, LPCTSTR lpszActivePrinter) {_WordApplication m_wordApp; // define application objects Documents m_wordDocs Word provided; // define _Document m_wordDoc Word document object provided; // current document object m_wordDocs.ReleaseDispatch ( ); M_worddoc.releaseDispatch (); m_wordapp.m_bautorelease = true; if (! M_wordapp.created ") // Create a Word Application Service {AFXMessageBox (" Create Word Application Service Failed! "); Return False; }
m_wordapp.setvisible (false); // Set to hide // below is the open file definition variant variable; Colevariant VarfilePath (LPSZFileName); Colevariant Varstrnull ("); Colevariant Varzero (Short); Colevariant Vartrue (Short (1 ), Vt_bool); Colevariant Varfalse (SHORT (0), VT_BOOL);
m_wordDocs.AttachDispatch (m_wordApp.GetDocuments ()); // the class object m_Docs Documents Idispatch interfaces and associate; m_wordDocs.Open (varFilePath, varFalse, varFalse, varFalse, varstrNull, varstrNull, varFalse, varstrNull, varstrNull, varTrue, varTrue, varTrue); m_wordDoc.AttachDispatch (m_wordApp.GetActiveDocument ()); // get the current active Document object m_wordApp.SetActivePrinter (lpszActivePrinter); // set the current printer COleVariant covTrue ((short) TRUE), covFalse ((short) FALSE) , covOptional ((long) DISP_E_PARAMNOTFOUND, VT_ERROR); // define the print attributes m_wordDoc.PrintOut (covFalse, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, COleVariant ((long) 1), covOptional, covOptional, covOptional, covOptional, CoVoptional, Covoptional, Covoptional, Covoptional, CoVoptional, C Ovoptional); M_WordApp.quit (Covoptional, Covoptional, Covoption); // Exit
M_WordDoc.releaseDispatch (); // Disconnected; m_worddocs.releaseDispatch (); m_wordapp.releaseDispatch (); return true;
}
The above is the operation of the corresponding document with Office, if you need to make a corresponding other actions, you can record with the macros in Office to record it to the corresponding code.
For Office operations in Java, you must call or use JAVA-COM's corresponding packages with JNI.