在用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();
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/do2jiang/archive/2009/04/17/4087132.aspx