用MFC做个Dialog Dialog里面有EDITControl里面的内容是从数据库中读取出来的 想将按一定格式其导入到WORD文档里 请问:如何操作。请给出一个简单的例子 谢谢~~~~

解决方案 »

  1.   

    给你个例子:网上有很多的,你自己搜下吧
    #include "msword9.h"
    #include <AtlBase.h>void CStep4Dlg::OnOK() 
    {
    _Application app;
    app.CreateDispatch(_T("Word.Application"));
    app.SetVisible(TRUE); Documents docs=app.GetDocuments();
    CComVariant Template(_T(""));
    CComVariant NewTemplate(false),DocumentType(0),Visible;
    docs.Add(&Template,&NewTemplate,&DocumentType,&Visible); Selection sel=app.GetSelection();
    sel.TypeText(_T("HELLO\r\n大家好呀")); AfxMessageBox(_T("好了,我要保存到c:\\hello.doc中了"));
    /**************** 这是在WORD中录制的新建文档直到另存的宏 *************
        Documents.Add Template:= _
            "C:\Documents and Settings\Administrator\Application Data\Microsoft\Templates\Normal.dot" _
            , NewTemplate:=False, DocumentType:=0
        Selection.TypeText Text:="Hello"
        Selection.TypeParagraph
        Selection.TypeText Text:="大家好"
        ChangeFileOpenDirectory "C:\"
        ActiveDocument.SaveAs FileName:="Hello.doc", FileFormat:=wdFormatDocument _
            , LockComments:=False, Password:="", AddToRecentFiles:=True, _
            WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
             SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
            False
    *********************************************************************//**************** 程序思路 ******************************************
    另存为的函数是ActiveDocument.SaveAs,显然表示的是对当前活跃的文档进行保存,
    在我们的类中没有ActiveDocument,其实它对应的是_Document,而这个可以由
    _Application 的GetActiveDocument()得到。你一定会提问:“你怎么知道的?”
    呵呵,怎么说那,我怎么知道的那?答案是:猜。其实如果使用的多了,分析、猜
    查找都是办法。如果想得到确切的方法,其实可以在VBA的书或微软的网站中搜索
    *********************************************************************/ _Document doc=app.GetActiveDocument(); //得到ActiveDocument
    CComVariant FileName(_T("c:\\Hello.doc")); //文件名
    CComVariant FileFormat(0); //重点,看下面的说明
    CComVariant LockComments(false),Password(_T(""));
    CComVariant AddToRecentFiles(true),WritePassword(_T(""));
    CComVariant ReadOnlyRecommended(false),EmbedTrueTypeFonts(false);
    CComVariant SaveNativePictureFormat(false),SaveFormsData(false);
    CComVariant SaveAsAOCELetter(false);
    /*************** FileFormat 文件格式说明 ****************************
    参数FileFormat,在WORD的宏中,使用的是 wdFormatDocument,这是什么那?
    其实这是WORD宏中所使用的常量,由匈牙利命名可以知道wd其实是DWORD的意思
    知道了是一个正数,那么它到底是多少那?其实有一个办法可以知道,那就是在
    WORD宏程序中,加一条语句:MsgBox wdFormatDocument 这样你再运行宏程序,
    就能看到这个常量是多少了。呵呵,这个常量是0,我够聪明吧^_^
    *********************************************************************/ doc.SaveAs(&FileName,&FileFormat,&LockComments,&Password,
    &AddToRecentFiles,&WritePassword,&ReadOnlyRecommended,
    &EmbedTrueTypeFonts,&SaveNativePictureFormat,&SaveFormsData,
    &SaveAsAOCELetter);

    sel.ReleaseDispatch();
    doc.ReleaseDispatch();
    docs.ReleaseDispatch(); CComVariant SaveChanges(false),OriginalFormat,RouteDocument;
    app.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
    app.ReleaseDispatch();
    AfxMessageBox(_T("请检查c:\\hello.doc是否正常产生了。下面该学习Setp5了"));