VC 操作Word 2003的简单例子

来源:互联网 发布:华为中央软件院 待遇 编辑:程序博客网 时间:2024/05/22 16:02

实现这种功能通常的做法便是调用Office提供的COM操作。一般你要在的office安装目录下找到一个名为“MSWORD.OLB” 的库文件,2003的话一般是放在“C:/program files/office/OFFICE11”这个路径下。找到它以后,你可以将其拷贝到自己的工程目录下,也可以直接使用它的文件路径,这就关系到import

里导入该库时的参数。在把代码拷贝到你的工作区以后,可以选择其中一个对document进行操作的函数,比如:“get_Documents”转到定义,你会发现出现了一个msword.tlh的文件,具体怎么产生的,可以自己研究一下,小弟这里仅通知你怎么怎么简单地使用它,当然了这其中有的地方转了点小弯,以便大家对它进行探索,否则光用了代码,没有收获是没有任何意义的。

下面说一下,因为每一个WORD版本里所提供的COM 函数的名字都不一样,但用法都是相似的,而且,你可以对一次对文档的操作录制一个宏,然后看它的宏代码,那里面所使用的函数和我们在VC里所要编辑的是一样的。可以每次操作后,分析宏代码,然后对应着写,收效较大。小声说一下,里面的函数使用是没有说明的,我都是根据宏里面的参数模仿着写下来的。在此,小弟只是进行了创建一个新的文档,并将一些数据写进去,然后将文件另存。其它具体的、复杂的应用,录个宏先,然后分析一下。。

Note:这里尤其要注意的是数据类型COleVariant,BSTR,因为用错了,效果就是没有!好了,闲言少叙,请看代码:  

#include   "afxdisp.h" 
#import    "MSWORD.OLB" named_guids raw_interfaces_only rename("ExitWindows", "wordExitWindows")  
/*
/;--------------------------------------------
/;  -COMMENT  -  Huang Zuosheng 2007-08-08    |
/;  -CONTENT  -  VC code that operate Word          + 
/;               process to generate notice                       +
/;               when a  transaction occurs                       |
/;  -FUNCTION -  'WriteToWord' create a new         +
/;               document and write words to                   +
/;               it based on the format that                        +
/;               we set.                                                           |
/;  -PARAMETER- 'contentText': the content             +
/;               text that you want to write                           +
/;              'relativelFileName': file                                +
/;               name without path and all of                    +
/;               files save in the folder                                +
/;     'word'                                                                        |
/;--------------------------------------------
*/
void   WriteContentToWordFile(const char* contentText,
        const char* relativelFileName)  
{
 CoInitialize(NULL);

//; basic info prepare for saving file     /
    the data type is special, 'COleVariant'.

 COleVariant   fileName(relativelFileName);   
 COleVariant   FileFormat(VARIANT_FALSE);  
 COleVariant   LockComments(VARIANT_FALSE);  
 COleVariant   Password("");  
 COleVariant   AddToRecentFiles(VARIANT_TRUE);  
 COleVariant   WritePassword("");
 COleVariant   ReadOnlyRecommended(VARIANT_FALSE);  
 COleVariant   EmbedTrueTypeFonts(VARIANT_FALSE);  
 COleVariant   SaveNativePictureFormat(VARIANT_FALSE);  
 COleVariant   SaveFormsData(VARIANT_FALSE);  
 COleVariant   SaveAsAOCELetter(VARIANT_FALSE);

 _variant_t   vOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);  
 _variant_t   vTrue(VARIANT_TRUE);  
 _variant_t   vFalse(VARIANT_FALSE);
    _variant_t   DocumentType(0);
 
    Word::_ApplicationPtr   app;  
 app.CreateInstance("Word.Application");  
 Word::DocumentsPtr   docs;   //;point to all the documents set
 Word::_DocumentPtr   doc;       //;point to a single document
 app->get_Documents(&docs);      //;get the documents set

//; create a new document that is pointed by the pointer 'doc'
 docs->Add (&vOptional, &vOptional, &vOptional, &vOptional, &doc);
 
 Word::SelectionPtr psSel;       //;a pointer to the selected document
    app->get_Selection(&psSel);  //;select the current document
 psSel->TypeParagraph();         //;a new paragraph

//; the text that you want to insert into document        /
 the string data type must be 'BSTR', others will fail.  
 CString lpszText="" ;           //;a common string based on 'char'
 lpszText= contentText;
//; convert string data type.
 BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText); 
    psSel->TypeText(bstrText);      //; Insert text to the document
//; get the directory that save the document that you edited.
 char direText[MAX_PATH]={0};
 ::GetCurrentDirectory(MAX_PATH,direText);
 CString lpszPath = direText;lpszPath+="//word//";
 BSTR bstrPath = _com_util::ConvertStringToBSTR(lpszPath); 
//; get the document and save it
 Word::_DocumentPtr oActiveDoc;      
 app->get_ActiveDocument(&oActiveDoc);
    app->ChangeFileOpenDirectory(bstrPath);
 oActiveDoc->SaveAs(&fileName,&FileFormat,&LockComments,
          &Password,&AddToRecentFiles,
    &WritePassword,&ReadOnlyRecommended,
    &EmbedTrueTypeFonts,&SaveNativePictureFormat,
    &SaveFormsData,&SaveAsAOCELetter);


 app->Quit(&vTrue,&vOptional,&vOptional);
 CoUninitialize();

}