unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, OleCtnrs,OleServer, word2000,ComCtrls,ComObj,activex,
Menus;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
OleContainer1: TOleContainer;
MainMenu1: TMainMenu;
est1: TMenuItem;
OPen1: TMenuItem;
Close1: TMenuItem;
GetPages1: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure OPen1Click(Sender: TObject);
procedure GetPages1Click(Sender: TObject);
procedure Close1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function Off_GetOleObject (const ClassName: string; var blCreate: Boolean): IDispatch;
{ 如果Word没有运行,则启动它,并返回一个自动化对象; 如果Word已经启动,就返回正在运行的实例的自动化对象。}
var ClassID: TGUID;
Unknown: IUnknown;
begin
blCreate := False;
ClassID := ProgIDToClassID(ClassName);
if Succeeded (GetActiveObject(ClassID, nil, Unknown)) then
OleCheck (Unknown.QueryInterface (IDispatch, Result))
else
begin
blCreate := True;
Result := CreateOleObject (ClassName);
end;
end;
function Off_GetWordDocPages(v_Parawd: OleVariant; v_Paradocs: OleVariant;
strFName: string; var strTitle: string;blOpenFile: Boolean): Boolean;
var v_Docs, v_wd, v_Name, v_Range, v_Text: OleVariant;
blWd, blDoc, blCreate: Boolean;
begin
blWd := False;
blDoc := False;
v_Docs := unassigned;
v_wd := unassigned;
v_Range := unassigned;
v_Text := unassigned;
v_Name := strFName;
try
try
{连接word}
if (VarIsEmpty(v_ParaWd)) then
begin
v_Wd := Off_GetOleObject('Word.Application', blCreate);
{打开文档}
v_Docs := v_Wd.Documents.Open(FileName:=v_Name, ReadOnly:=True,AddToRecentFiles:=False);
blWd := True;
blDoc := True;
//FWordApplication := v_Wd;
end
else
begin
v_Wd := v_ParaWd;
if (not VarIsEmpty(v_ParaDocs)) then
v_Docs := v_ParaDocs
else
begin
if (not blOpenFile) and (v_Wd.Documents.Count > 0) then
v_Docs := v_Wd.ActiveDocument
else
begin
v_Docs := v_Wd.Documents.Open(FileName:=v_Name, ReadOnly:=True, AddToRecentFiles:=False);
blDoc := True;
end;
end;
end;
{提取Title}
v_Text := v_Docs.BuiltInDocumentProperties[wdPropertyPages];
strTitle := v_Text;
if (blDoc) and (not VarIsEmpty(v_Docs)) then
v_Docs.Close(wdDoNotSaveChanges, wdOriginalDocumentFormat, EmptyParam);
Result := True;
except
Result := False;
end;
finally
if (blWd) and (blCreate) and (not VarIsEmpty(v_Wd)) then
v_Wd.quit;
v_Wd := unassigned;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if (not OpenDialog1.Execute) then Exit;
Edit1.Text := OpenDialog1.FileName;
end;
procedure TForm1.OPen1Click(Sender: TObject);
begin
OleContainer1.CreateObjectFromFile(Edit1.Text, False);
end;
procedure TForm1.GetPages1Click(Sender: TObject);
var v_obj, v_wd: OleVariant;
s: string;
begin
v_obj := OleContainer1.OleObject;
v_wd := v_obj.Application;
Off_GetWordDocPages(v_wd, unassigned, Edit1.Text, s, False);
Application.MessageBox(PChar('共有' + s + '页'), '提 示', MB_OK + MB_ICONINFORMATION);
end;
procedure TForm1.Close1Click(Sender: TObject);
begin
OleContainer1.Close;
end;
end.