用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧编程其他

用bcb实 现Word 97 自 动 化

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:41:13

  ---- 要 在 应 用 程 序 中 控 制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 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
 #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) ;
file://Word 没 启 动 就 启 动 它 返 回 一 自 动 化 对 象
 }
else  
Obj1=GetActiveOleObject(AppName);
 // 否 则 返 回 正 在 运 行 的 实 例 自 动 化 对 象
Obj1.OlePropertySet("Visible",true);
return Obj1;
  }
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int j;
V=GetWordObject();
  // 获 得Word 97 自 动 化 对 象Application
Vdocuments=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 中 编 译
通 过, 运 行 该 应 用 程 序 首 先 必 须 获 得 文 档, 然 后 才 可 以 编
辑 文 档。


 


Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:14,390.63000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号