本文跟大家分享的是VC操作Word的东东,使用的是Office2000,工程类型是MFC的对话框。

       先看看需要使用到的东东: 1、View->ClassWizard->Automation->Add Class From Library:定位到你安装Office的目录找到MSWord9.Olb;2、打开后把所有的类都选中倒入到你的工程(反正也是搞一回彻底一点:P) ;3、在你的对话框头文件中加上#include"msword9.h"。OK了准备工作搞定之后就可以玩儿转Word了。

       一、VC简单操作Word

       先来一个简单的

C++代码
  1. void TestWord1()  
  2. {  
  3. _Application app;  
  4. COleVariant vTrue((short)TRUE), vFalse((short)FALSE);  
  5. app.CreateDispatch(_T("Word.Application"));  
  6. app.SetVisible(FALSE);  
  7. //Create New Doc  
  8. Documents docs=app.GetDocuments();  
  9. CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);  
  10. docs.Add(&tpl,&NewTemplate,&DocType,&Visble);  
  11. //Add Content:Text  
  12. Selection sel=app.GetSelection();  
  13. sel.TypeText(_T("\t\t\t\t\t第一次玩儿Word\r\n"));  
  14. sel.TypeText(_T("\t\t\t\t\t\t\t\t----------先来一个简单的\r\n"));  
  15. sel.ReleaseDispatch();  
  16. docs.ReleaseDispatch();  
  17. app.SetVisible(TRUE);  
  18. app.ReleaseDispatch();  
  19. }  

       二、VC操作Word生成表格

       大家都应该使用过Word提供的表格功能很是专业,如果在作一些数据库开发之类的软件对汇总的数据进行打印是常见的功能,这个如果用Word来实现个人感觉不错,废话少说说练咱就练。

C++代码
  1. void CWordTestDlg::TestWord2()  
  2. {  
  3. _Application app;  
  4. COleVariant vTrue((short)TRUE), vFalse((short)FALSE);  
  5. app.CreateDispatch(_T("Word.Application"));  
  6. app.SetVisible(FALSE);  
  7. //Create New Doc  
  8. Documents docs=app.GetDocuments();  
  9. CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);  
  10. docs.Add(&tpl,&NewTemplate,&DocType,&Visble);  
  11. //Add Content:Text  
  12. Selection sel=app.GetSelection();  
  13. sel.TypeText(_T("\t\t\t\t\t\t\t情况汇总\r\n"));  
  14. sel.TypeText(_T("\t\t\t\t\t\t\t\t----------*******跨国公司\r\n"));  
  15. COleDateTime dt=COleDateTime::GetCurrentTime();  
  16. CString strDT=dt.Format("%Y-%m-%d");  
  17. CString str("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");  
  18. str+=strDT;  
  19. str+="\r\n";  
  20. sel.TypeText(str);  
  21. //Add Table  
  22. _Document saveDoc=app.GetActiveDocument();  
  23. Tables tables=saveDoc.GetTables();  
  24. CComVariant defaultBehavior(1),AutoFitBehavior(1);  
  25. tables.Add(sel.GetRange(),6,11,&defaultBehavior,&AutoFitBehavior);  
  26. Table table=tables.Item(1);  
  27. sel.TypeText(_T("Test1"));  
  28. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short  
  29. (0)));  
  30. sel.TypeText(_T("Test2"));  
  31. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  32. sel.TypeText(_T("Test3"));  
  33. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  34. sel.TypeText(_T("Test4"));  
  35. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short<(0)));  
  36. sel.TypeText(_T("Test5"));  
  37. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  38. sel.TypeText(_T("Test6"));  
  39. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  40. sel.TypeText(_T("Test7"));  
  41. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  42. sel.TypeText(_T("Test8"));  
  43. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  44. sel.TypeText(_T("Test9"));  
  45. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  46. sel.TypeText(_T("Test10"));  
  47. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  48. sel.TypeText(_T("Test11"));  
  49. app.SetVisible(TRUE);  
  50. table.ReleaseDispatch();  
  51. tables.ReleaseDispatch();  
  52. sel.ReleaseDispatch();  
  53. docs.ReleaseDispatch();  
  54. saveDoc.ReleaseDispatch();  
  55. app.ReleaseDispatch();  
  56. }  

       上面说了如何制作一个简单的表格,在实际应用中会发现表格的种类很多其中对于单元格要求合并的情形很多,这样出来的表格比较专业让客户看起来也很是舒服,不喔喔了看看下面的代码就全明白了。

C++代码
  1. void CWordTestDlg::TestWord3()  
  2. {  
  3. _Application app;  
  4. COleVariant vTrue((short)TRUE), vFalse((short)FALSE);  
  5. app.CreateDispatch(_T("Word.Application"));  
  6. app.SetVisible(FALSE);  
  7. //Create New Doc  
  8. Documents docs=app.GetDocuments();  
  9. CComVariant tpl(_T("")),Visble,DocType(0),NewTemplate(false);  
  10. docs.Add(&tpl,&NewTemplate,&DocType,&Visble);  
  11. //Add Content:Text  
  12. Selection sel=app.GetSelection();  
  13. sel.TypeText(_T("\t\t\t\t\t\t\t情况汇总\r\n"));  
  14. sel.TypeText(_T("\t\t\t\t\t\t\t\t----------*******跨国公司\r\n"));  
  15. COleDateTime dt=COleDateTime::GetCurrentTime();  
  16. CString strDT=dt.Format("%Y-%m-%d");  
  17. CString str("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");  
  18. str+=strDT;  
  19. str+="\r\n";  
  20. sel.TypeText(str);  
  21. //Add Table  
  22. _Document saveDoc=app.GetActiveDocument();  
  23. Tables tables=saveDoc.GetTables();  
  24. CComVariant defaultBehavior(1),AutoFitBehavior(1);  
  25. tables.Add(sel.GetRange(),7,11,&defaultBehavior,&AutoFitBehavior);  
  26. Table table=tables.Item(1);  
  27. sel.TypeText(_T("Test1"));  
  28. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  29. sel.TypeText(_T("Test2"));  
  30. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  31. sel.TypeText(_T("Test3"));  
  32. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  33. sel.TypeText(_T("Test4"));  
  34. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  35. sel.TypeText(_T("Test5"));  
  36. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  37. sel.TypeText(_T("Test6"));  
  38. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  39. sel.TypeText(_T("Test7"));  
  40. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  41. sel.TypeText(_T("Test8"));  
  42. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  43. sel.TypeText(_T("Test9"));  
  44. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  45. sel.TypeText(_T("Test10"));  
  46. sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));  
  47. sel.TypeText(_T("Test11"));  
  48. for(int i=2;i<7;i+=2)  
  49. {  
  50. Cell c1=table.Cell(i,1);  
  51. Cell c2=table.Cell(i+1,1);  
  52. c1.Merge(c2);  
  53. c1.ReleaseDispatch();  
  54. c2.ReleaseDispatch();  
  55. }  
  56. app.SetVisible(TRUE);  
  57. table.ReleaseDispatch();  
  58. tables.ReleaseDispatch();  
  59. sel.ReleaseDispatch();  
  60. docs.ReleaseDispatch();  
  61. saveDoc.ReleaseDispatch();  
  62. app.ReleaseDispatch();  
  63. app.SetVisible(TRUE);  
  64. }  

        运行下看看效果,还不错吧?呵呵

除非特别注明,鸡啄米文章均为原创
转载请标明本文地址:http://www.jizhuomi.com/software/341.html
2013-10-30 16:13:10
作者:鸡啄米 分类:软件开发 浏览: 评论:0