void CVCForWordDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知處理程序代碼
COleVariant covZero((short)0),
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
covDocxType((short)0);
// 定義word變量
CApplication wordApp; // wordApp
CDocuments docxs; // docxs
CDocument0 docx, docx_active; // docx
if ( !wordApp.CreateDispatch(_T("Word.Application")) ) // 實(shí)例化wordApp,必須有初始化
{
AfxMessageBox(_T("本機(jī)沒有安裝word產(chǎn)品!"));
return;
}
else
{
wordApp.put_Visible(FALSE); // 設(shè)置文檔開始不可見
CString wordVersion = wordApp.get_Version(); // 獲得當(dāng)前word的版本,比如word2010為14.0,2013為15.0
// ****************** 添加一個document ******************
// 得到docxs
docxs = wordApp.get_Documents(); // 或者下面一段
// =====================================
//LPDISPATCH disp = wordApp.get_Documents();
//if ( NULL == disp )
// return;// FALSE;
//docxs.AttachDispatch(disp);
//if ( NULL == docxs.m_lpDispatch )
// return;// FALSE;
// =====================================
// 添加一個docx
docx = docxs.Add(covOptional, covOptional, covOptional, covOptional); // 未用模板時,或者下面段兩種
// =====================================
// 2,未用模板
//docx.AttachDispatch(docxs.Add(covOptional, covOptional, covOptional, covOptional));
// 3,使用模板
//CComVariant tpl(_T("")), Visble, DocxType(0), NewTemplate(false);
//docx = docxs.Add(&tpl,&NewTemplate,&DocxType,&Visble);
// =====================================
if ( NULL == docx.m_lpDispatch )
return;
// ****************** 設(shè)置頁邊距 ******************
// 放在創(chuàng)建文檔后,需要CPageSetup.h
docx_active = wordApp.get_ActiveDocument();
CPageSetup oPageSetup = docx_active.get_PageSetup();
// 設(shè)置為頁面方向和頁邊距
if ( oPageSetup.get_Orientation() == 0 ) // 若為縱向則設(shè)置為橫向,縱向wdOrientPortrait=0,橫向wdOrientLandscape=1
{
oPageSetup.put_Orientation(1); // 橫向
// 設(shè)置上下左右變距,單位緹,以下參數(shù)設(shè)置的頁邊距是“適中”
oPageSetup.put_TopMargin( (float) 72); // 適中時72=2.54cm,默認(rèn)時90=3.17cm;10≈0.35cm
oPageSetup.put_BottomMargin( (float) 72); // 適中時72=2.54cm,默認(rèn)時90=3.17cm;10≈0.35cm
oPageSetup.put_LeftMargin( (float) 54); // 適中時54=1.9cm,默認(rèn)時72=2.54cm
oPageSetup.put_RightMargin( (float) 54); // 適中時54=1.9cm,默認(rèn)時72=2.54cm
}
//else // 設(shè)置為縱向
//{
// oPageSetup.put_Orientation(0);
// // 設(shè)置上下左右變距,單位緹,以下參數(shù)設(shè)置的頁邊距是“適中”
// oPageSetup.put_TopMargin( (float) 72); // 適中時72=2.54cm,默認(rèn)或普通時72=2.54cm;10≈0.35cm
// oPageSetup.put_BottomMargin( (float) 72); // 適中時72=2.54cm,默認(rèn)或普通時72=2.54cm;10≈0.35cm
// oPageSetup.put_LeftMargin( (float) 54); // 適中時54=1.9cm,默認(rèn)或普通時90=3.17cm
// oPageSetup.put_RightMargin( (float) 54); // 適中時54=1.9cm,默認(rèn)或普通時90=3.17cm
//}
// 聲明一個CSelection對象,并實(shí)例化
CSelection wordSelection = wordApp.get_Selection();
// ****************** 設(shè)置文檔內(nèi)容 ******************
wordSelection.TypeText(_T("虛擬試驗仿真報表"));
wordSelection.HomeKey(COleVariant((short)5), COleVariant((short)1)); // wdLine=5,返回當(dāng)前行首,并選擇當(dāng)前行
wordSelection.put_Style( COleVariant((short)-2) );// 設(shè)置為“標(biāo)題1“樣式,wdStyleHeading1=-2
// 設(shè)置選擇區(qū)域字體,一定要放在樣式后,否則格式會被樣式的覆蓋
CFont0 font = wordSelection.get_Font();
font.put_Name(_T("微軟雅黑"));
font.put_Size(16); // 必須選擇該行才可以修改,即必須有HomeKey那行
// 獲得當(dāng)前段落,并設(shè)置對齊方式
CParagraph lastPara = GetCurParagraph(docx);
lastPara.put_Alignment(1); // wdAlignParagraphLeft=0, wdAlignParagraphCenter=1,wdAlignParagraphRight=2
// 結(jié)束當(dāng)前段落編輯,移動光標(biāo)到段落后
wordSelection.EndOf(COleVariant((short)4), COleVariant((short)0)); // wdParagraph=4,wdMove=0
wordSelection.TypeParagraph(); // 新起一段
COleVariant covTime(_T("yyyy-MM-dd:dddd")); // 時間格式可調(diào)整
wordSelection.InsertDateTime(covTime, covFalse, covOptional, covOptional, covOptional); // 插入當(dāng)前時間
wordSelection.EndOf(COleVariant((short)4), COleVariant((short)0)); //結(jié)束當(dāng)前段落編輯,wdParagraph=4,wdMove=0
// 生成表格
MakeRTITable( docx, wordSelection );
// 以下為為不同段落設(shè)置不同字體和對齊方式示例
wordSelection.TypeParagraph(); // 新起一段
wordSelection.TypeText(_T("end of the story!"));
wordSelection.HomeKey(COleVariant((short)5), COleVariant((short)1)); // wdLine=5,返回當(dāng)前行首,并選擇當(dāng)前行
/*CFont0 */font = wordSelection.get_Font();
font.put_Size(20); // 必須選擇該行才可以修改,即必須有HomeKey那行
/*CParagraph */lastPara = GetCurParagraph(docx);
lastPara.put_Alignment(3); // 右對齊
wordSelection.EndOf(COleVariant((short)4), COleVariant((short)0)); //結(jié)束當(dāng)前段落編輯,wdParagraph=4,wdMove=0
wordSelection.TypeParagraph(); // 新起一段
wordSelection.TypeText(_T("Thanks for reading!"));
wordSelection.HomeKey(COleVariant((short)5), COleVariant((short)1)); // wdLine=5,返回當(dāng)前行首,并選擇當(dāng)前行
/*CFont0 */font = wordSelection.get_Font();
font.put_Size(10); // 必須選擇該行才可以修改,即必須有HomeKey那行
font.put_Name(_T("Times New Roman"));
/*CParagraph */lastPara = GetCurParagraph(docx);
lastPara.put_Alignment(1); // 居中對齊
wordSelection.EndOf(COleVariant((short)4), COleVariant((short)0)); //結(jié)束當(dāng)前段落編輯,wdParagraph=4,wdMove=0
// 插入分頁符,用于換頁
wordSelection.InsertBreak(covOptional);
// 插入公式,操作域
CFields fields = wordSelection.get_Fields();
COleVariant ofont = _variant_t(_T("Times New Roman"));
COleVariant text = _variant_t(_T("EQ \\a \\ar \\co2 \\vs3 \\hs3(Axy,Bxy,A,B)")); // 注意要兩個\\,一個轉(zhuǎn)義后不對!!!
fields.Add( wordSelection.get_Range(), covOptional, text, covFalse );
wordSelection.HomeKey(COleVariant((short)5), COleVariant((short)1)); // wdLine=5,返回當(dāng)前行首,并選擇當(dāng)前行
lastPara = GetCurParagraph(docx);
lastPara.put_Alignment(0); // 左對齊
wordSelection.EndOf(COleVariant((short)4), COleVariant((short)0)); //結(jié)束當(dāng)前段落編輯,wdParagraph=4,wdMove=0
// 獲取應(yīng)用當(dāng)前Debug路徑
char fileName[MAX_PATH];
GetModuleFileName(NULL, fileName, MAX_PATH);
char dir[260];
char dirver[100];
_splitpath(fileName, dirver, dir, NULL, NULL);
CString strAppPath = dirver;
strAppPath += dir;
//CString strAppPath = _T("D:\\");
// ****************** 插入圖片示例 ******************
// 需要CWindow0.h, CPane0.h, CView0.h
wordSelection.TypeParagraph(); // 另起一段
CString strPicture = strAppPath + _T("\\截圖.jpg");
CnlineShapes nLineShapes = wordSelection.get_InlineShapes();
CnlineShape nLineshape = nLineShapes.AddPicture(strPicture, covFalse, covTrue, covOptional);
// ****************** 設(shè)置頁眉頁腳 ******************
CWindow0 oWind = docx.get_ActiveWindow();
CPane0 oPane = oWind.get_ActivePane(); // 一定將CPane改為CPane0或其他
CView0 oView = oPane.get_View();
// =============== 設(shè)置頁眉 ===============
oView.put_SeekView(9); // wdSeekCurrentPageHeader=9
/*CFont0 */font = wordSelection.get_Font(); // 設(shè)置選擇區(qū)域字體
font.put_Name(_T("華文楷體"));
font.put_Size(16);
/*CParagraphFormat */lastPara = wordSelection.get_ParagraphFormat(); // 默認(rèn)為居中
lastPara.put_Alignment(1); // wdAlignParagraphLeft=0, wdAlignParagraphCenter=1, wdAlignParagraphRight=2
wordSelection.TypeText(_T("網(wǎng)絡(luò)大學(xué)"));
// =============== 設(shè)置頁腳,包括頁碼 ===============
oView.put_SeekView(10); // wdSeekCurrentPageFooter=10
/*CFont0 */font = wordSelection.get_Font(); // 設(shè)置選擇區(qū)域字體,一定要放在樣式后,否則格式會被樣式的覆蓋
font.put_Name(_T("華文楷體"));
font.put_Size(16);
/*CParagraphFormat */lastPara = wordSelection.get_ParagraphFormat(); // 默認(rèn)為居中
lastPara.put_Alignment(1); // wdAlignParagraphLeft=0, wdAlignParagraphCenter=1, wdAlignParagraphRight=2
// 添加頁碼
wordSelection.TypeText(_T("第頁 共頁"));
wordSelection.MoveLeft( COleVariant((short)1), COleVariant((short)4), &covZero );
/*CFields */fields = wordSelection.get_Fields();
fields.Add( wordSelection.get_Range(), COleVariant((short)33), COleVariant("PAGE "), &covTrue ); // 增加頁碼域,當(dāng)前頁碼
wordSelection.MoveRight( COleVariant((short)1), COleVariant((short)3), &covZero);
fields.Add( wordSelection.get_Range(), COleVariant((short)26), COleVariant("NUMPAGES "), &covTrue ); // 增加頁碼域,總頁數(shù)
oView.put_SeekView(0); // 關(guān)閉頁眉頁腳,wdSeekMainDocument=0,回到主控文檔
// Word程序可見,顯示報表
//wordApp.put_Visible(TRUE);
// 保存成果
CString strSavePath = strAppPath;
strSavePath += _T("\\報表.docx");
docx.SaveAs(COleVariant(strSavePath), covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,covOptional);
// 退出word應(yīng)用
docx.Close(covFalse, covOptional, covOptional);
wordApp.Quit(covOptional, covOptional, covOptional);
wordApp.ReleaseDispatch();
MessageBox(_T("生成成功!"));
}
}