用C++Builder实现Word 97自动化

    技术2022-05-11  128

    用C++Builder实现Word 97自动化江西南昌华东交大控制信息工程研究所(330013)刘遵雄

      ----要在应用程序中控制Word 97的运行,首先必须在编制自动化客户程序时使其头文件中包含文件ComObj.hpp,写入#include。编程工具C++Builder的开发者把调用Word 97自动化对象的功能大致包装成了几个Ole object class函数,应用程序设计人员可以很方便地利用它们。

     *设定Ole对象的属性:        OlePropertyGet(propname)    OlePropertySet(propname,value)  其中value是任何可以转换为Variant型的值

     *调用Ole对象的方法:        OleProcedure(OleProcName,[val,...])    OleFunction(OleFuncName,[val,...])   其中val是任何可以转换为Variant型的值

      ----可以通过OleFunction(OleFuncName,[val,...])父对象返回其子对象,句法是:子对象名=父对象名. OleFunction(OleFuncName,val)。而且可以对此方法组合从父对象返回其下层对象的对象。

      ---- C++Builder中使用OLE自动化控制Word 97,必须掌握Word 97的自动化对象及Microsoft Word Visual Basic帮助文件中关于Word的对象方法和属性。Word对象代表一个Word的元素,如文档、段落、书签或单个的字符。集合是一个对象,该对象包含其他数个对象,通常这些对象属于相同的类型,例如,一个集合对象中可包含文档中的所有书签对象。通过使用属性和方法,可以修改单个的对象,也可修改整个的对象集合。属性是对象的一个特性或者该对象操作的一个方面。例如,文档属性包含名称、内容、保存状态,以及是否启用修订。要更改一个对象的属性,可以修改属性的值。方法是对象可以进行的动作。

      ----代表Word 97应用程序的自动化对象有两个:Word.Application和Word.Basic,通过Application对象的WordBasic属性可以允许它们之间进行变换赋值。在C++Builder中通过CreateOleObject("…")就可启动Word并获得句柄,将其赋给Variant变量。如有两个Variant型变量V1和V2,WordBasic是Application对象的属性:    V1=CreateOleObject("Word.Application");    V2=V1.OleFunction("WordBasic")。

      ----以下通过一个简单的例子,具体说明如何在C++Builder中实现Word 97的自动化功能,其功能是打开或创建新文档,并向当前活动中发送文字、插入位图和画条直线等。在C++Builder集成开发环境IDE下,建立一项目Project1,其中包含Form1(其Caption为OLE Automation)的单元文件Unit1.cpp。表单Form1的OpenDialog1对象的Filter设为Word文档或位图。Unit1.cpp的头文件中必须包含"ComObj.hpp"。代码如下:头文件Unit1.h代码为:

    #ifndef Unit1H#define Unit1H//-------------#include #include #include #include #include #include #include //-------------  class TForm1 : public TForm{__published:      // IDE-managed Components    TButton*Button1;    TOpenDialog*OpenDialog1;    TButton*Button2;    TMemo*Memo1;    TBevel*Bevel1;    TButton*Button3;    TLabel*Label1;    TButton*Button5;    TBevel*Bevel2;    TLabel*Label2;    TLabel*Label3;    void __fastcall Button1Click(TObject*Sender);    void __fastcall Button3Click(TObject*Sender);    void __fastcall Button2Click(TObject*Sender);    void __fastcall Button5Click(TObject*Sender);private:// User declarationspublic:// User declarations    Variant V,Vdocuments,Vdocument1,Vdocument2,Vactive; /*定义Variant型全局//变量, 分别指代Application对象, V的Documents 对象, Vdocuments的Document对象(Vdocument1,Vdocument2)以及   V的ActiveDocument对象*/    int tag; //用于记录Button3被点击的次数    __fastcall TForm1(TComponent*Owner);    Variant __fastcall GetWordObject();    //获得Word 97自动化对象函数    void __fastcall Add(int i);    //编辑当前活动文档指定的     段落文字的函数};//-------------extern PACKAGE TForm1*Form1;//-------------#endifUnit1.cpp文件代码为:#include #pragma hdrstop#include "Unit1.h"#include //-------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1*Form1;//-------------__fastcall TForm1::TForm1(TComponent*Owner)    : TForm(Owner){tag=1;//令tag的初值为1,  其将随Button3的点击次数而递增}//-------------Variant __fastcall TForm1::GetWordObject(){Variant Obj1;AnsiString AppName="Word.Application" ;HWND hPrevApp = ::FindWindow(NULL,                   "Microsoft Word"); if(!hPrevApp) {Obj1=CreateOleObject(AppName) ;//Word没启动就启动它返回一自动化对象 }else   Obj1=GetActiveOleObject(AppName);  //否则返回正在运行的实例自动化对象Obj1.OlePropertySet("Visible",true);return Obj1;  }void __fastcall TForm1::Button1Click(TObject*Sender){ int j;V=GetWordObject();  //获得Word 97自动化对象ApplicationVdocuments=V.OleFunction("Documents");  //通过Application获取Documents对象if (OpenDialog1->Execute())//使用Documents对象的Open方法 打开文件,并返回Document对象Vdocument1=Vdocuments.OleFunction   ("Open",OpenDialog1->FileName);j=Vdocument1.OleFunction("ComputeStatistics",2);  //计算打开文档的页数Label1->Caption="文件"+ Vdocument1.OlePropertyGet("Name")  +"页数是:"+IntToStr(j); }//-------------void __fastcall TForm1::Button3Click(TObject*Sender){ int i,j; i=tag; Vactive=V.OleFunction("ActiveDocument"); //获取Application的ActiveDocument对象 j=Vactive.OleFunction("ComputeStatistics",4); //计算当前文档的段落数 //的的Paragraphs集合对象增加一段Vactive.OleFunction("Paragraphs").OleFunction("Add"); i=j+i;//当前文档被编辑的段落序号 Add(i);//调用编辑当前活动文档       指定的段落文字的函数 Memo1->Clear();//清除Memo1的内容 tag=tag+1;}//--------------void __fastcall TForm1::Button2Click(TObject*Sender){V=GetWordObject();Vdocuments=V.OleFunction("");Vdocument2=Vdocuments.OleFunction("Add"); //使用Documents对象的Add方法新建文档Vdocument2.OlePropertySet("Password","12345");  //设置新建文档的口令}//--------------void __fastcall TForm1::Add(int i){Variant V1;//设置被编辑段的内容、字体大小、  颜色及是否斜体V1=((Vactive.OleFunction("Paragraphs")).    OleFunction("Item",i)).OleFunction("Range");(V1.OleFunction("Font")).OlePropertySet("Italic",true);(V1.OleFunction("Font")).OlePropertySet("Size",18);(V1.OleFunction("Font")).OlePropertySet("ColorIndex",6);(V1.OleFunction("Font")).OlePropertySet("Size",18); V1.OlePropertySet("Text",Memo1->Text);}//-------------void __fastcall TForm1::Button5Click(TObject*Sender){Variant Vshape,Vactive1,Vline;Vactive1=V.OleFunction("ActiveDocument");Vshape=Vactive1.OleFunction("Shapes");//获取当前活动文档的Shapes对象//使用Shapes对象的AddLine方法化直线Vline=Vshape.OleFunction("AddLine",90,80,400,80);if (OpenDialog1->Execute())//使用Shapes对象的AddPicture   方法插入指定位图  Vshape.OleFunction("AddPicture",OpenDialog1  ->FileName,50,50,80,100);}

      ----此程序在C++Builder 3中编译通过,运行该应用 程序首先必须获得文档,然后才可以编辑文档。


    最新回复(0)