VC利用WORD替换功能打印发票

    技术2022-05-11  18

    VC利用WORD替换功能打印发票

     

    摘自网络

     

    在用VC开发一项目时,需要打印发票。由于发票的种类比较多,而且有的是已经有文字,有的空白的,还有一部分文字是可变的,动态改变。如果用VC直接控制它的输出,是一件很烦人的事情,而且工作量巨大,因此考虑到officeword文字操作软件有杰出的文字操作功能,并且它有相关的VBA函数用于二次开发。在VC中当然也会提供相应的操作函数,只要电脑安装了word,只需要将其它导入开发程序即可。

    相关思路是,在word中做好一个发票模板,然后将那些需要动态改的文字设成特定的标志,在VC中用word的替换功能将其替换,这样可以省去很多的麻烦,简化操作。这样一来,发票的格式,文字位置就可以简单地通过改变WORD模板的设计而改变了。

     

    //前期准备  参考 博客文章 Visual C++ 中操纵 MS Word 123

    创建(或打开已有的)一个 MFC 的程序工程2  Ctrl+W 执行 ClassWizard->automation-> Add Class.../From a type Library... Office2000 目录中,找到MSWORD9.OLB  C:/Program Files/Microsoft Office/Office/MSWORD9.OLB(该文件名根据版本不同会有所区别)->选择需要的类,或者用鼠标和Shift键配合,全部选择也可以。初始化COM。方法一,找到AppInitInstance()函数,在其中添加 AfxOleInit()函数的调用在需要调用 Office 功能函数的 cpp 文件中     #include <atlbase.h>  // 为了方便操作 VARIANT 类型变量,使用 CComVariant 模板类     #include "头文件.h"   // 具体的头文件名,是由装载类型库的文件名决定的。(鼠标双点包装类的文件,就可以看到)                           // 比如使用 msword9.olb类型库,那么头文件是 msword9.h

    //相关变量 Find fndInDoc;  Range myRange;  _Application myApp;  Documents myDocs;  _Document myDoc;  Replacement rpInDoc;

    //打开word myApp.CreateDispatch("Word.Application");  myApp.SetVisible(TRUE); 

    //打开文档

    /***********获得绝对路径********************************************/ CString fileName("test.doc"); TCHAR exeFullPath[MAX_PATH];     CString strPath;     GetModuleFileName(NULL,exeFullPath,MAX_PATH);     strPath=(CString)exeFullPath;     int position=strPath.ReverseFind('//');     strPath=strPath.Left(position+1);   fileName=strPath+fileName;/****************************************************************/

    // COleVariant FileName("C://test.doc");//注意写文件名全路径          //如果文件找不到会出现错误 COleVariant FileName(fileName);//test.doc放在当前路径

     COleVariant vTrue((short)TRUE),  vFalse((short)FALSE),  vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); myDocs=myApp.GetDocuments(); myDoc=myDocs.Add(FileName,vOpt,vOpt,vOpt);

    //利用替换功能,相关参数的选择最困难 myRange=myDoc.GetContent();  fndInDoc=myRange.GetFind();  fndInDoc.ClearFormatting();   rpInDoc=fndInDoc.GetReplacement();  rpInDoc.ClearFormatting();   CString replaceStr("#用户姓名#");//被替换 CString replaceStrWith("邱秋十九");//替换

     COleVariant Text(replaceStr); //被替换 COleVariant MatchCase((short)FALSE);  COleVariant MatchWholeWord((short)FALSE);  COleVariant MatchWildcards((short)FALSE);  COleVariant MatchSoundsLike((short)FALSE);  COleVariant MatchAllWordForms((short)FALSE);  COleVariant Forward((short)TRUE);  COleVariant Wrap((short)1);//msgbox(wdFindContinue)得到  COleVariant format((short)FALSE);  COleVariant ReplaceWith=(replaceStrWith);//替换  COleVariant Replace((short)2);//msgbox(wdReplaceAll)得到 COleVariant MatchKashida=((short)FALSE); //以下四个参数默认false COleVariant MatchDiacritics=((short)FALSE);  COleVariant MatchAlefHamza=((short)FALSE);  COleVariant MatchControl=((short)FALSE);  fndInDoc.Execute(&Text, &MatchCase, &MatchWholeWord, &MatchWildcards,   &MatchSoundsLike, &MatchAllWordForms, &Forward, &Wrap,   &format, &ReplaceWith, &Replace, &MatchKashida,   &MatchDiacritics, &MatchAlefHamza, &MatchControl);

    //打印 COleVariant covTrue((short)TRUE),  covFalse((short)FALSE),  covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); // myDoc.PrintPreview();// 如果你希望打印前预览文档,加上这句// AfxMessageBox("请预览"); if(MessageBox("是否打印","打印",MB_ICONEXCLAMATION|MB_OKCANCEL)==IDOK)  {  myDoc.PrintOut(covFalse,              // Background.   covOptional,           // Append.   covOptional,           // Range.   covOptional,           // OutputFileName.   covOptional,           // From.   covOptional,           // To.   covOptional,           // Item.   COleVariant((long)1),  // Copies.   covOptional,           // Pages.   covOptional,           // PageType.   covOptional,           // PrintToFile.   covOptional,           // Collate.   covOptional,           // ActivePrinterMacGX.   covOptional,           // ManualDuplexPrint.   covOptional,           // PrintZoomColumn  New with Word 2002   covOptional,           // PrintZoomRow          ditto   covOptional,           // PrintZoomPaperWidth   ditto   covOptional);          // PrintZoomPaperHeight  ditto*/ }//关闭 CComVariant SaveChanges(false),OriginalFormat,RouteDocument; myApp.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);

    //数据清空 myRange.ReleaseDispatch(); fndInDoc.ReleaseDispatch(); rpInDoc.ReleaseDispatch(); myDocs.ReleaseDispatch(); myDoc.ReleaseDispatch();

     


    最新回复(0)