VC对Office进行操作.docx

上传人:自*** 文档编号:126245596 上传时间:2020-03-23 格式:DOCX 页数:9 大小:37.26KB
返回 下载 相关 举报
VC对Office进行操作.docx_第1页
第1页 / 共9页
VC对Office进行操作.docx_第2页
第2页 / 共9页
VC对Office进行操作.docx_第3页
第3页 / 共9页
VC对Office进行操作.docx_第4页
第4页 / 共9页
VC对Office进行操作.docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《VC对Office进行操作.docx》由会员分享,可在线阅读,更多相关《VC对Office进行操作.docx(9页珍藏版)》请在金锄头文库上搜索。

1、用VC对Office进行操作的介绍已经不少了,但是从来没有把word,excel,powerPoint进进全面的介绍的。由于工作的需要,我需要对在自己的软件中对word,excel,powerPoint进行操作。所以把自己的体会写出来和大家分享,希望对大家有所帮助。当然还有很多不当之处,希望大家指出。用例子来说明吧,首先创建一个MFC AppWizard(EXE)工程,然后通过在VIEW菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 选中Microsoft Office 2000 类型库:Excel9.olb,M

2、SPPT9.OLB,MSWORD9.OLB(在Microsoft OfficeOffice目录下) 会将类型库中的所有类添加到你的工程中。 然后写一个类来操作Office吧!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 1

3、000#pragma once#endif / _MSC_VER 1000 #include comdef.h#include ObtGuiGcomMsWord9.h#include ObtGuiGcomMsPpt9.h#include ObtGuiGcomExcel9.hclass AFX_EXT_CLASS ObtGuiGcomOfficePrinter public:ObtGuiGcomOfficePrinter();virtual ObtGuiGcomOfficePrinter();/Operatorpublic:BOOL WordPrinterToJcf(LPCTSTR lpszFi

4、leName,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 ne

5、w DEBUG_NEW#endif/ Construction/Destruction/ObtGuiGcomOfficePrinter:ObtGuiGcomOfficePrinter()ObtGuiGcomOfficePrinter:ObtGuiGcomOfficePrinter()BOOL ObtGuiGcomOfficePrinter:PowerPointPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)_PptApplication m_powerpointApp;Presentations m_powerpointP

6、res;_Presentation m_powerpointPre;m_powerpointPres.ReleaseDispatch();m_powerpointPre.ReleaseDispatch();if(!m_powerpointApp.CreateDispatch(PowerPoint.Application) AfxMessageBox(创建PowerPoint服务失败!); return FALSE; m_powerpointApp.m_bAutoRelease=true;m_powerpointApp.SetVisible(TRUE);/对于PowerPoint必须设置为TRU

7、Em_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_powerpoi

8、ntPres.ReleaseDispatch();m_powerpointApp.ReleaseDispatch();return TRUE;BOOL ObtGuiGcomOfficePrinter:ExcelPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)_ExcelApplication m_excelApp;/定义Excel提供的应用程序对象Workbooks m_excelBooks; _Workbook m_excelBook;m_excelBooks.ReleaseDispatch();m_excelBook.

9、ReleaseDispatch();m_excelApp.m_bAutoRelease=true;/创建Excel 2000服务器(启动Excel) if (!m_excelApp.CreateDispatch(Excel.Application) AfxMessageBox(创建Excel服务失败!); return FALSE; m_excelApp.SetVisible(FALSE); /设置为隐藏/利用模板文件建立新文档 m_excelBooks.AttachDispatch(m_excelApp.GetWorkbooks(),true); m_excelBook.AttachDisp

10、atch(m_excelBooks.Add(_variant_t(lpszFileName); /m_excelApp.SetActivePrinter(lpszActivePrinter); /设置当前打印机COleVariant covTrue(short)TRUE), covFalse(short)FALSE), covOptional(long)DISP_E_PARAMNOTFOUND, VT_ERROR);/定义打印机并打印m_excelBook.PrintOut(covOptional,covOptional,COleVariant(long(1),covFalse,covOpti

11、onal,covOptional,covOptional,covOptional);m_excelApp.Quit();/退出m_excelBook.ReleaseDispatch(); m_excelBooks.ReleaseDispatch(); m_excelApp.ReleaseDispatch(); return TRUE;BOOL ObtGuiGcomOfficePrinter:WordPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)_WordApplication m_wordApp;/定义Word提供的应用

12、程序对象Documents m_wordDocs;/定义Word提供的文档对象_Document m_wordDoc; /当前的的文档对象m_wordDocs.ReleaseDispatch();m_wordDoc.ReleaseDispatch();m_wordApp.m_bAutoRelease=true;if(!m_wordApp.CreateDispatch(Word.Application) /创建Word应用服务 AfxMessageBox(创建Word应用服务失败!); return FALSE; m_wordApp.SetVisible(FALSE); /设置为隐藏/下面是打开

13、文件定义VARIANT变量;COleVariant varFilePath(lpszFileName);COleVariant varstrNull();COleVariant varZero(short)0);COleVariant varTrue(short(1),VT_BOOL);COleVariant varFalse(short(0),VT_BOOL);m_wordDocs.AttachDispatch(m_wordApp.GetDocuments();/将Documents类对象m_Docs和Idispatch接口关联起来;m_wordDocs.Open(varFilePath,v

14、arFalse,varFalse,varFalse,varstrNull,varstrNull,varFalse,varstrNull,varstrNull,varTrue,varTrue,varTrue);m_wordDoc.AttachDispatch(m_wordApp.GetActiveDocument(); /得到当前激活的Document对象m_wordApp.SetActivePrinter(lpszActivePrinter); /设置当前打印机COleVariant covTrue(short)TRUE), covFalse(short)FALSE), covOptional(long)DISP_E_PARAMNOTFOUND, VT_ERROR); /定义打印属性m_wordDoc.PrintOut(covFalse, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, COleVariant(long)1), covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOp

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号