⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 o20.htm

📁 C++ Builder学习中文版,让学者对其有一定的理解.
💻 HTM
字号:
 

<html>

<head>

<title>用C++Builder实现Word97自动化</title>

<meta http-equiv="目录类型" content="文本/html; 字符集=gb2312">

</head>

<body bgcolor="#FFFFFF">

<table width="100%" border="0" height="285">

  <tr> 

    <td height="35"> 

      <div align="center" class="p14"><font color="#000000">用C     

        + + Builder 实 现Word 97 自 动 化</font></div>    

    </td>    

  </tr>    

  <tr valign="top">     

    <td>   

      <p><font color="#000000">---- 要 在 应 用 程 序 中 控 制Word 97 的 运 行, 首 先 必     

        须 在 编 制 自 动 化 客 户 程 序 时 使 其 头 文 件 中 包 含 文 件ComObj.hpp, 写 入 #include <comobj.hpp>。     

        编 程 工 具C + + Builder 的 开 发 者 把 调 用Word 97 自 动 化 对 象 的 功 能 大 致 包 装 成 了     

        几 个Ole object class 函 数, 应 用 程 序 设 计 人 员 可 以 很 方 便 地 利 用 它 们。</font> </p>    

      <p><font color="#000000">* 设 定Ole 对 象 的 属 性:</font> </p>    

      <pre><font color="#000000">    OlePropertyGet(propname)

    OlePropertySet(propname,value)

   其 中value 是 任 何 可 以 转 换 为Variant 型 的 值

</font></pre>   

      <p><font color="#000000">* 调 用Ole 对 象 的 方 法:</font> </p>    

      <pre><font color="#000000">    OleProcedure(OleProcName,[val,...])

    OleFunction(OleFuncName,[val,...])

    其 中val 是 任 何 可 以 转 换 为Variant 型 的 值

</font></pre>   

      <p><font color="#000000">---- 可 以 通 过OleFunction(OleFuncName,[val,...])     

        父 对 象 返 回 其 子 对 象, 句 法 是: 子 对 象 名= 父 对 象 名. OleFunction(OleFuncName,val)。     

        而 且 可 以 对 此 方 法 组 合 从 父 对 象 返 回 其 下 层 对 象 的 对 象。</font> </p>    

      <p><font color="#000000">---- C + + Builder 中 使 用OLE 自 动 化 控 制Word     

        97, 必 须 掌 握Word 97 的 自 动 化 对 象 及Microsoft Word Visual Basic 帮 助 文 件 中     

        关 于Word 的 对 象 方 法 和 属 性。Word 对 象 代 表 一 个 Word 的 元 素, 如 文 档、 段 落、 书 签 或     

        单 个 的 字 符。 集 合 是 一 个 对 象, 该 对 象 包 含 其 他 数 个 对 象, 通 常 这 些 对 象 属 于 相 同 的     

        类 型, 例 如, 一 个 集 合 对 象 中 可 包 含 文 档 中 的 所 有 书 签 对 象。 通 过 使 用 属 性 和 方 法,     

        可 以 修 改 单 个 的 对 象, 也 可 修 改 整 个 的 对 象 集 合。 属 性 是 对 象 的 一 个 特 性 或 者 该 对     

        象 操 作 的 一 个 方 面。 例 如, 文 档 属 性 包 含 名 称、 内 容、 保 存 状 态, 以 及 是 否 启 用 修 订。     

        要 更 改 一 个 对 象 的 属 性, 可 以 修 改 属 性 的 值。 方 法 是 对 象 可 以 进 行 的 动 作。</font> </p>    

      <p><font color="#000000">---- 代 表Word 97 应 用 程 序 的 自 动 化 对 象 有 两 个:Word.Application     

        和 Word.Basic, 通 过Application 对 象 的WordBasic 属 性 可 以 允 许 它 们 之 间 进 行 变     

        换 赋 值。 在C + +Builder 中 通 过CreateOleObject(" …") 就 可 启 动Word 并 获 得 句 柄,     

        将 其 赋 给Variant 变 量。 如 有 两 个Variant 型 变 量V1 和V2, WordBasic 是Application     

        对 象 的 属 性:</font> </p>    

      <pre><font color="#000000">    V1=CreateOleObject(&quot;Word.Application&quot;);

    V2=V1.OleFunction(&quot;WordBasic&quot;)。

</font></pre>   

      <p><font color="#000000">---- 以 下 通 过 一 个 简 单 的 例 子, 具 体 说 明 如 何 在C     

        + + Builder 中 实 现 Word 97 的 自 动 化 功 能, 其 功 能 是 打 开 或 创 建 新 文 档, 并 向 当     

        前 活 动 中 发 送 文 字、 插 入 位 图 和 画 条 直 线 等。 在C + + Builder 集 成 开 发 环 境IDE 下,     

        建 立 一 项 目Project1, 其 中 包 含Form1( 其Caption 为OLE Automation) 的 单 元 文 件Unit1.cpp。     

        表 单Form1 的OpenDialog1 对 象 的Filter 设 为 Word 文 档 或 位 图。Unit1.cpp 的 头 文 件     

        中 必 须 包 含"ComObj.hpp"。 代 码 如 下:</font> </p>    

      <pre><font color="#000000"> 头 文 件Unit1.h 代 码 为:

 #ifndef Unit1H

 #define Unit1H

// - - - - - - - - - - - - -

 #include <classes.hpp>

 #include <controls.hpp>

 #include <stdctrls.hpp>

 #include <forms.hpp>

 #include <comobj.hpp>

 #include <dialogs.hpp>

 #include <extctrls.hpp>

// - - - - - - - - - - - - -

  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 declarations

public:// 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;

// - - - - - - - - - - - - -

 #endif

Unit1.cpp 文 件 代 码 为:

 #include <vcl.h>

 #pragma hdrstop

 #include &quot;Unit1.h&quot;

 #include <comobj.hpp>

// - - - - - - - - - - - - - 

 #pragma package(smart_init)

 #pragma resource &quot; *.dfm&quot;

TForm1 *Form1;

// - - - - - - - - - - - - - 

__fastcall TForm1::TForm1(TComponent * Owner)

    : TForm(Owner)

{

tag=1;// 令tag 的 初 值 为1,

   其 将 随Button3 的 点 击 次 数 而 递 增

}

// - - - - - - - - - - - - -

Variant __fastcall TForm1::GetWordObject()

{

Variant Obj1;

AnsiString AppName=&quot;Word.Application&quot; ;

HWND hPrevApp = ::FindWindow(NULL, 

                  &quot;Microsoft Word&quot;);

 if(!hPrevApp)

 {

Obj1=CreateOleObject(AppName) ;

//Word 没 启 动 就 启 动 它 返 回 一 自 动 化 对 象

 }

else   

Obj1=GetActiveOleObject(AppName); 

 // 否 则 返 回 正 在 运 行 的 实 例 自 动 化 对 象

Obj1.OlePropertySet(&quot;Visible&quot;,true);

return Obj1;

  }

void __fastcall TForm1::Button1Click(TObject *Sender)

{

 int j;

V=GetWordObject();

  // 获 得Word 97 自 动 化 对 象Application

Vdocuments=V.OleFunction(&quot;Documents&quot;); 

 // 通 过Application 获 取Documents 对 象

if (OpenDialog1 -&gt;Execute())

// 使 用Documents 对 象 的Open 方 法 

  打 开 文 件, 并 返 回Document 对 象

Vdocument1=Vdocuments.OleFunction

   (&quot;Open&quot;,OpenDialog1 -&gt;FileName);

j=Vdocument1.OleFunction(&quot;ComputeStatistics&quot;,2);

  // 计 算 打 开 文 档 的 页 数

Label1 -&gt;Caption=&quot; 文 件&quot; +

 Vdocument1.OlePropertyGet(&quot;Name&quot;) 

  +&quot; 页 数 是:&quot; +IntToStr(j);

 }

// - - - - - - - - - - - - -

void __fastcall TForm1::Button3Click(TObject *Sender)

{

 int i,j;

 i=tag;

 Vactive=V.OleFunction(&quot;ActiveDocument&quot;);

 // 获 取Application 的ActiveDocument 对 象

 j=Vactive.OleFunction(&quot;ComputeStatistics&quot;,4);

 // 计 算 当 前 文 档 的 段 落 数

 // 的 的Paragraphs 集 合 对 象 增 加 一 段

Vactive.OleFunction(&quot;Paragraphs&quot;).OleFunction(&quot;Add&quot;);

 i=j +i;// 当 前 文 档 被 编 辑 的 段 落 序 号

 Add(i);// 调 用 编 辑 当 前 活 动 文 档

        指 定 的 段 落 文 字 的 函 数

 Memo1 -&gt;Clear();// 清 除Memo1 的 内 容

 tag=tag +1;

}

// - - - - - - - - - - - - - - 

void __fastcall TForm1::Button2Click(TObject *Sender)

{

V=GetWordObject();

Vdocuments=V.OleFunction(&quot;&quot;);

Vdocument2=Vdocuments.OleFunction(&quot;Add&quot;);

 // 使 用Documents 对 象 的Add 方 法 新 建 文 档

Vdocument2.OlePropertySet(&quot;Password&quot;,&quot;12345&quot;);

  // 设 置 新 建 文 档 的 口 令

}

// - - - - - - - - - - - - - - 

void __fastcall TForm1::Add(int i)

{

Variant V1;

// 设 置 被 编 辑 段 的 内 容、 字 体 大 小、

   颜 色 及 是 否 斜 体

V1=((Vactive.OleFunction(&quot;Paragraphs&quot;)).

    OleFunction(&quot;Item&quot;,i)).OleFunction(&quot;Range&quot;);

(V1.OleFunction(&quot;Font&quot;)).OlePropertySet(&quot;Italic&quot;,true);

(V1.OleFunction(&quot;Font&quot;)).OlePropertySet(&quot;Size&quot;,18);

(V1.OleFunction(&quot;Font&quot;)).OlePropertySet(&quot;ColorIndex&quot;,6);

(V1.OleFunction(&quot;Font&quot;)).OlePropertySet(&quot;Size&quot;,18);

 V1.OlePropertySet(&quot;Text&quot;,Memo1 -&gt;Text);

}

// - - - - - - - - - - - - - 

void __fastcall TForm1::Button5Click(TObject *Sender)

{

Variant Vshape,Vactive1,Vline;

Vactive1=V.OleFunction(&quot;ActiveDocument&quot;);

Vshape=Vactive1.OleFunction(&quot;Shapes&quot;);

// 获 取 当 前 活 动 文 档 的Shapes 对 象

// 使 用Shapes 对 象 的AddLine 方 法 化 直 线

Vline=Vshape.OleFunction(&quot;AddLine&quot;,90,80,400,80);

if (OpenDialog1 -&gt;Execute())

// 使 用Shapes 对 象 的AddPicture 

   方 法 插 入 指 定 位 图

  Vshape.OleFunction(&quot;AddPicture&quot;,OpenDialog1

   -&gt;FileName,50,50,80,100);

}

</font></pre>   

      <p><font color="#000000">----此 程 序 在C + + Builder 3 中 编 译 通 过, 运 行     

        该 应 用 程 序 首 先 必 须 获 得 文 档, 然 后 才 可 以 编 辑 文 档。</font> </p>    

     </td>    

  </tr>    

</table>    

<br>    

</body>    

</html>    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -