C++、MFC往Word模板中添加图片
新建一个基于对话框的MFC应用程序工程,将对话框界面设置成图1所示。
“类向导”界面如图2,在 “添加类”处选择“类型库中的MFC类(T)...”,弹出图3所示界面,选择从文件中添加类。
![](https://i-blog.csdnimg.cn/blog_migrate/90483afb9785da34a7708fbcbd413332.png)
![](https://i-blog.csdnimg.cn/blog_migrate/215e8509a210289c16df862b36cb0c75.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1c46efffb4fe52abb07c5cd8ae6e5305.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7f8494eb9bb3e82e277f785666e4afb0.png)
![](https://i-blog.csdnimg.cn/blog_migrate/215e6879fc95df9bc6b37394a6980e76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3b9a270ce8cdeb6a6197ba7cccfd3d80.png)
if (!AfxOleInit())
{
AfxMessageBox(_T("无法初始化COM的动态链接库!"));
return FALSE;
}
![](https://i-blog.csdnimg.cn/blog_migrate/274d653f7628357efb98ee3bb6f1bda7.png)
代码添加后会出现图12所示,获取路径的错误,在项目-属性-配置属性-常规-字符集中,将项目的字符集改为“使用多字节字符集”,如图13所示。则该错误得到解决。
![](https://i-blog.csdnimg.cn/blog_migrate/97ee4ece7d803dfa9842f031ef6f78e3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e408399dfe9ad0f26643611fe89a6049.png)
![](https://i-blog.csdnimg.cn/blog_migrate/afc449616db2e8804b868aafee5bde58.png)
![](https://i-blog.csdnimg.cn/blog_migrate/076f8e6649b43f594d70d01561401855.png)
// 获取应用当前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:\\");
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"))) // 实例化wordApp,必须有初始化
{
AfxMessageBox(_T("本机没有安装word产品!"));
return;
}
else
{
wordApp.put_Visible(FALSE); // 设置文档开始不可见
CString wordVersion = wordApp.get_Version(); // 获得当前word的版本,比如word2010为14.0,2013为15.0
docxs = wordApp.get_Documents(); // 得到docxs
docx = docxs.Add(covOptional, covOptional, covOptional, covOptional); // 添加一个docx
CComVariant tpl(strAppPath + _T("\\Template.dot")), Visble, DocxType(0), NewTemplate(false);
docx = docxs.Add(&tpl, &NewTemplate, &DocxType, &Visble);
if (NULL == docx.m_lpDispatch)
return;
CSelection wordSelection = wordApp.get_Selection();// 声明一个CSelection对象,并实例化
// 定义table和cell变量
CTable0 table;
CTables0 tables;
CCell cell;
//填写表格
wordSelection = wordApp.get_Selection(); //获取光标位置,一般在文首
wordSelection.MoveDown(COleVariant((short)5), COleVariant((short)2), COleVariant((short)0));
tables = docx.get_Tables();
table = tables.Item(tables.get_Count());
//插入图片
//wordSelection.TypeParagraph(); // 另起一段
cell = table.Cell(1, 1);
cell.Select(); //选中该单元
CString strPicture1 = strAppPath + _T("\\Result\\潘春春.jpg");
CnlineShapes nLineShapes1 = wordSelection.get_InlineShapes();
CnlineShape nLineshape1 = nLineShapes1.AddPicture(strPicture1, covFalse, covTrue, covOptional);
nLineshape1.put_Height(478);
nLineshape1.put_Width(550);
//wordApp.put_Visible(TRUE);// Word程序可见,显示报告
// 保存word文档
CString strSavePath = strAppPath;
strSavePath += _T("\\Word.docx");
docx.SaveAs(COleVariant(strSavePath), covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
// 退出word应用
docx.Close(covFalse, covOptional, covOptional);
wordApp.Quit(covOptional, covOptional, covOptional);
wordApp.ReleaseDispatch();
MessageBox(_T("生成成功!"));
}
本文对应的资源已上传至https://download.csdn.net/download/fei_12138/13033821