原文:http://blog.csdn.net/hhhh63/article/details/7652696
每隔一二年就要用一次 WORD 或其它 MSOffice 组件,好多次了,每次都忘的一干二净,从头google,这次又要用,顺便记下来,以备不时之需,同时分享给大家。开发环境 WIN7-X64, VC2010, MSOffice2010。
1. 在VC2010项目名称上右击,选择类向导...
2.在右上角的添加类中选择“类库中的MFC类”
3. 在新对话框中选择文件,点击右边的“...”按钮,按图示选择MSWORD.OLD文件。
4. 按需要选择一些常用的类,完成
5. 把新生成的 .h 文件顶部的 #import "E:\\TTMv8\\Debug\\MSWORD.OLB" no_namespace 这一行注释掉,否则编译时会出现一大堆错误,网上有别的解决办法,我觉得注掉这个办法最好。出现的错误代码如下:
//#import "E:\\TTMv8\\Debug\\MSWORD.OLB" no_namespace
/* 编译时出一大堆错,把上面这一句注消就好了!!!!!!!,可能还能保证各word版本的兼容性
1>e:\ttmv8\ttmv8\capplication.h(3): warning C4278: “ExitWindows”: 类型库“E:\\TTMv8\\Debug\\MSWORD.OLB”中的标识符已经是宏;使用“rename”限定符
1>e:\ttmv8\ttmv8\capplication.h(3): error C2872: “Font”: 不明确的符号
1> 可能是“c:\program files (x86)\microsoft visual studio 10.0\vc\include\comdef.h(312) : Font”
1> 或 “c:\program files (x86)\microsoft sdks\windows\v7.0a\include\gdiplusheaders.h(244) : Gdiplus::Font”
1>e:\ttmv8\ttmv8\capplication.h(3): error C2872: “Font”: 不明确的符号
1> 可能是“c:\program files (x86)\microsoft visual studio 10.0\vc\include\comdef.h(312) : Font”
1> 或 “c:\program files (x86)\microsoft sdks\windows\v7.0a\include\gdiplusheaders.h(244) : Gdiplus::Font”
*/
6. 添加代码,以下是实际使用的代码,功能是用指定的 word 模板新建一个文档,在预设的书签位置插入文字和图片。
// 将当前所有打开的图像窗口导出到word中, 最多输出6个
- void ViewImagesFrame::OnReportimg()
- {
- CDocument0 cdoc;
-
- if( m_bViewmax )
- {
-
- CString str;
- str.Format( L"%s\\imgreport1.dotx", theApp.appPath);
- if( ! wordopen( str, cdoc ) )
- return;
-
-
- insertinfo( cdoc );
-
-
- str.Format(L"%s/reportimg0.png", theApp.appPath);
- m_pView->OnExportimg( str );
- insertimg(str, cdoc, L"v8img_image0");
- }
- else
- {
- int n = m_arViews.GetSize();
- if( !n )
- return;
-
- if( n > 6 )
- n = 6;
-
-
- CString str;
- str.Format( L"%s\\imgreport%i.dotx", theApp.appPath, n);
- if( ! wordopen( str, cdoc ) )
- return;
-
-
- insertinfo( cdoc );
-
-
- for( int i = 0; i < n; i++ )
- {
- str.Format(L"%s/reportimg%i.png", theApp.appPath, i);
- m_arViews[i]->OnExportimg( str );
- CString str2;
- str2.Format(L"v8img_image%i", i);
- insertimg(str, cdoc, str2);
- }
- }
- }
-
-
- bool ViewImagesFrame::wordopen( LPCTSTR dotxname, CDocument0 &cdoc )
- {
- COleVariant
- covTrue((short)TRUE),
- covFalse((short)FALSE),
- covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
-
- covFileName(dotxname);
-
- CApplication capp;
- CDocuments cdocs;
- if(FALSE == capp.CreateDispatch(_T("word.application"), NULL)){
- return false;
- }
-
- capp.put_Visible(TRUE);
-
- CString version = capp.get_Version();
- CString name = capp.get_Name();
- CString cap1 = capp.get_Caption();
-
- LPDISPATCH disp = capp.get_Documents();
-
- if(NULL == disp){
- return false;
- }
-
- cdocs.AttachDispatch(disp);
-
- if(NULL == cdocs.m_lpDispatch){
- return false;
- }
-
- cdoc.AttachDispatch(cdocs.Add(
- covFileName,
- covFalse,
- covFalse,
- covTrue
- ));
-
- if(NULL == cdoc.m_lpDispatch){
- return false;
- }
-
- return true;
- }
-
-
- void ViewImagesFrame::insertinfo( CDocument0 &cdoc )
- {
- myimg * pim = m_arViews[0]->pimg;
-
- insertstr(cdoc, L"v8img_name", pim->name );
-
- CString str;
- str.Format(L"%i", pim->m_nAge);
- insertstr(cdoc, L"v8img_age", str );
-
- insertstr(cdoc, L"v8img_sex", pim->m_strSex );
-
- insertstr(cdoc, L"v8img_subject", pim->subject );
-
- str = pim->scanday.Format( _T("%Y-%m-%d") );
- insertstr(cdoc, L"v8img_scandate", str );
-
- insertstr(cdoc, L"v8img_sick", pim->sickid );
-
- Cdasys ds;
- insertstr(cdoc, L"v8img_company", ds.getvalue(L"CompanyName") );
- insertstr(cdoc, L"v8img_address", ds.getvalue(L"CompanyAddress") );
- insertstr(cdoc, L"v8img_telephone", ds.getvalue(L"CompanyTelephone") );
- }
-
-
- void ViewImagesFrame::insertstr( CDocument0 &cdoc, LPCTSTR strMark, LPCTSTR strValue )
- {
- CBookmarks bms = cdoc.get_Bookmarks();
-
- COleVariant bm( strMark );
- CBookmark0 bk = bms.Item(bm);
- CRange rang = bk.get_Range();
- rang.InsertAfter(strValue);
- }
-
-
- void ViewImagesFrame::insertimg( LPCTSTR imgname, CDocument0 &cdoc, LPCTSTR mark )
- {
- CBookmarks bms = cdoc.get_Bookmarks();
-
- COleVariant bm0( mark );
- CBookmark0 bk = bms.Item(bm0);
- CRange rang = bk.get_Range();
- CnlineShapes shape = cdoc.get_InlineShapes();
- shape.AddPicture(imgname, COleVariant((short)FALSE), COleVariant((short)TRUE), &_variant_t(rang));
- }