我想作一个数据库的程序,将数据库中的数据打印到WORD文件中去。请问,怎样让程序自动生成WORD文档,并且将数据库中的数据输出到WORD文档中去?因为我想在打印的数据前加入一些报告信息,所以这样设计。

解决方案 »

  1.   

    possible
    search q178749 in msdn, and also refer to the following
    void CMailMergeDlg::OnRun() 
    {
        //For optional arguments
        COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);
        //Start Word
        _Application oApp;
        oApp.CreateDispatch("Word.Application");
        if(!oApp)
        {
            AfxMessageBox("Cannot start Word.");
            return;
        }
        //Get the Documents collection so that you can add
        //a new Document for the mail merge "main document"
        Documents oDocs = oApp.GetDocuments();
       _Document oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
        //Get the MailMerge property of the new document
        MailMerge oMMerge = oDoc.GetMailMerge();    
        //Set the document type as Form Letters
        oMMerge.SetMainDocumentType((long)0);   //wdFormLetters = 0
        //Set up the mail merge data source to the DSN "Northwind" and
        //an SQL statement
        CString sSQL= "SELECT * from Table1";
    // COleVariant ovar_connection("DSN=MS Access Database;DBQ=F:\\MASTERZ\\db2002.mdb;FIL=MS Access;");
    COleVariant ovar_connection("table table1");
    CString sname="F:\\MASTERZ\\db2002.mdb";
    oMMerge.OpenDataSource(sname,//name
    vOpt, //Format
    vOpt, //ConfirmConversions
    vOpt, //ReadOnly
    vOpt,//LinkToSource
    vOpt, //AddToRecentFiles
    vOpt, //PasswordDocument
    vOpt, //PasswordTemplate
    vOpt, //Revert
    vOpt, //WritePasswordDocument
    vOpt,//WritePasswordTemplate
    ovar_connection,//Connection vOpt,//
    vOpt,//COleVariant(sSQL), //SQLStatement 
    vOpt, //SQLStatement1
    vOpt, //OpenExclusive
    vOpt);//SubType
        //Add the field codes and text to the document
        Selection oSel = oApp.GetSelection();
        Range oRange;    MailMergeFields oMMFlds = oMMerge.GetFields();
        MailMergeField oMMFld;    oRange = oSel.GetRange();
        oMMFld = oMMFlds.Add(oRange, "CompanyName");
        oSel.TypeParagraph();
        oRange = oSel.GetRange();
        oMMFld = oMMFlds.Add(oRange, "Address");
        oSel.TypeParagraph();//    oRange = oSel.GetRange();
    //    oMMFld = oMMFlds.Add(oRange, "City");
    //    oSel.TypeText(", ");
    //    oRange = oSel.GetRange();
    //    oMMFld = oMMFlds.Add(oRange, "Country");
    //    oSel.TypeParagraph();
    //    oSel.TypeParagraph();
    //    oSel.TypeText("Dear ");
    //    oRange = oSel.GetRange();
    //    oMMFld = oMMFlds.Add(oRange, "ContactName");
    //    oSel.TypeText(",");
    //    oSel.TypeParagraph();
    //    oSel.TypeParagraph();
    //    oSel.TypeText("This letter is to inform you...");
    //    oSel.TypeParagraph();
        oSel.TypeParagraph();
        oSel.TypeText("Sincerely, John Smith");
        //Execute the mail merge and then close the
        //main document without saving changes
        oMMerge.SetDestination(0); //wdSendToNewDocument = 0
        oMMerge.Execute(COleVariant((short)FALSE));
        oDoc.Close(COleVariant((short)FALSE), vOpt, vOpt);
        //Make Word visible
        oApp.SetVisible(TRUE);

    }
      

  2.   

    给你一段MSDN上的源码
    COleVariant vTrue((short)TRUE),
    vFalse((short)FALSE),
    vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //Start a new instance of Microsoft Word
    _Application oWordApp;
    if (!oWordApp.CreateDispatch("Word.Application", NULL))
    {
    AfxMessageBox("CreateDispatch failed.", 
      MB_OK | MB_SETFOREGROUND);
    return;
    } try
    {   //Create a new document
      Documents oDocs;
      _Document oDoc;
      oDocs = oWordApp.GetDocuments();
      oDoc = oDocs.Add(vOpt, vOpt);
      PageSetup mPageSetup;
          mPageSetup=oDoc.GetPageSetup();//Öмä¼ÓÕâÒ»¾ä£¬Ïȵõ½ÎĵµµÄPagesetup
      mPageSetup.SetOrientation(1);
      oDoc.SetPageSetup(mPageSetup);
      //Add text to the document
      Selection oSel;
      oSel = oWordApp.GetSelection();
      int i=0;
      while (i<100)
      {
      oSel.TypeText("one");
      oSel.TypeParagraph();
      oSel.TypeText("two");
      oSel.TypeParagraph();
          oSel.TypeText("three");
      i++;
      }   //Save the document
      _Document oActiveDoc; 
      oActiveDoc = oWordApp.GetActiveDocument();
      oWordApp.SetVisible(TRUE);
      // TRY THIS:
      // To see the error handling in action, change
      // the following line to:
      //
      // oActiveDoc.SaveAs(COleVariant((long)5), 
      //                   COleVariant((short)0),
      //                   vFalse, COleVariant(""), vTrue, COleVariant(""),
      //                   vFalse, vFalse, vFalse, vFalse, vFalse);
      //
      // ALSO TRY THIS:
      // To see the error handling in action, change the
      // following line to:
      //
      // oActiveDoc.SaveAs(COleVariant("c:\\badpath\\doc1.doc"), 
      //                   COleVariant((short)0),
      //                   vFalse, COleVariant(""), vTrue, COleVariant(""),
      //                   vFalse, vFalse, vFalse, vFalse, vFalse);   oActiveDoc.SaveAs(COleVariant("c:\\doc1.doc"), 
    COleVariant((short)0),
    vFalse, COleVariant(""), vTrue, COleVariant(""),
    vFalse, vFalse, vFalse, vFalse, vFalse);
       }
        
       catch(COleException *e)
       {
      LPVOID lpMsg;
      ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
     FORMAT_MESSAGE_FROM_SYSTEM | 
     FORMAT_MESSAGE_IGNORE_INSERTS, NULL, e->m_sc,
     MAKELANGID(LANG_NEUTRAL, 
     SUBLANG_DEFAULT),(LPTSTR) &lpMsg,
     0, NULL);
      ::MessageBox(NULL, (LPCTSTR)lpMsg, "COM Error", 
      MB_OK | MB_SETFOREGROUND);
      ::LocalFree( lpMsg );
       }    catch(COleDispatchException *e)
       {
      char msg[512];
      sprintf(msg, "Run-time error '%d':\n\n%s", 
       e->m_scError & 0x0000FFFF, e->m_strDescription);
      ::MessageBox(NULL, msg, "Server Error", 
      MB_OK | MB_SETFOREGROUND);      
       }
       //Quit the application
       oWordApp.Quit(vFalse, vOpt, vOpt);