程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 在應用程序中控制Word 97的運行

在應用程序中控制Word 97的運行

編輯:關於C++

首先必須在編制自動化客戶程序時使其頭文件中包含文件ComObj.hpp,寫入#include <ComObj.hpp>。編程工具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 <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 "Unit1.h"
#include <ComObj.hpp>
//---------------------
#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自動化對象
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中編譯通過,運行該應用程序首先必須獲得文檔,然後才可以編輯文檔。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved