VC利用WORD替換功能列印發票

來源:互聯網
上載者:User

VC利用WORD替換功能列印發票

 

摘自網路

 

在用VC開發一項目時,需要列印發票。由於發票的種類比較多,而且有的是已經有文字,有的空白的,還有一部分文字是可變的,動態改變。如果用VC直接控制它的輸出,是一件很煩人的事情,而且工作量巨大,因此考慮到office的word文字操作軟體有傑出的文字操作功能,並且它有相關的VBA函數用於二次開發。在VC中當然也會提供相應的操作函數,只要電腦安裝了word,只需要將其它匯入開發程式即可。

相關思路是,在word中做好一個發票模板,然後將那些需要動態改的文字設成特定的標誌,在VC中用word的替換功能將其替換,這樣可以省去很多的麻煩,簡化操作。這樣一來,發票的格式,文字位置就可以簡單地通過改變WORD模板的設計而改變了。

 

//前期準備  參考 部落格文章 Visual C++ 中操縱 MS Word 123

1  建立(或開啟已有的)一個 MFC 的程式工程
2  Ctrl+W 執行 ClassWizard->automation-> Add Class.../From a type Library... 在 Office2000 目錄中,找到MSWORD9.OLB  C:/Program Files/Microsoft Office/Office/MSWORD9.OLB(該檔案名稱根據版本不同會有所區別)->選擇需要的類,或者用滑鼠和Shift鍵配合,全部選擇也可以。
3  初始化COM。方法一,找到App的InitInstance()函數,在其中添加 AfxOleInit()函數的調用
4  在需要調用 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();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.