home *** CD-ROM | disk | FTP | other *** search
/ PC Expert 29 / Pce29cd.iso / RUNIMAGE / DELPHI40 / DEMOS / ACTIVEX / OLEAUTO / WORD8 / WORD97.PAS < prev    next >
Pascal/Delphi Source File  |  1998-06-16  |  10KB  |  325 lines

  1. unit Word97;
  2.  
  3. interface
  4.  
  5. // This Demo excersises the use of ActiveX Automation using Early Binding.
  6. // For that reason, you will have to import the MSWord8.OLB, residing in
  7. // the Microsoft Office directory, prior to compiling this demo otherwise
  8. // you will get the error "File not found: Word_TLB.dcu"
  9.  
  10. uses
  11.   Windows, Classes, ActiveX, Word_TLB;
  12.  
  13. type
  14.   TWordEventSink = class(TInterfacedObject, IUnknown, IDispatch)
  15.   private
  16.     FOwner : TObject;
  17.     FAppDispatch: IDispatch;
  18.     FDocDispatch: IDispatch;
  19.     FAppDispIntfIID: TGUID;
  20.     FDocDispIntfIID: TGUID;
  21.     FAppConnection: Integer;
  22.     FDocConnection: Integer;
  23.     FOnQuit : TNotifyEvent;
  24.     FOnDocumentChange : TNotifyEvent;
  25.     FOnNewDocument : TNotifyEvent;
  26.     FOnOpenDocument : TNotifyEvent;
  27.     FOnCloseDocument : TNotifyEvent;
  28.   protected
  29.     { IUnknown }
  30.     function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
  31.     function _AddRef: Integer; stdcall;
  32.     function _Release: Integer; stdcall;
  33.     { IDispatch }
  34.     function GetTypeInfoCount(out Count: Integer): HRESULT; stdcall;
  35.     function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HRESULT; stdcall;
  36.     function GetIDsOfNames(const IID: TGUID; Names: Pointer;
  37.       NameCount, LocaleID: Integer; DispIDs: Pointer): HRESULT; stdcall;
  38.     function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
  39.       Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HRESULT; stdcall;
  40.   public
  41.     constructor Create(AnOwner: TObject; AnAppDispatch: IDispatch; const AnAppDispIntfIID, ADocDispIntfIID: TGUID);
  42.     destructor Destroy; override;
  43.     property OnQuit : TNotifyEvent read FOnQuit write FOnQuit;
  44.     property OnDocumentChange : TNotifyEvent read FOnDocumentChange write FOnDocumentChange;
  45.     property OnNewDocument : TNotifyEvent read FOnNewDocument write FOnNewDocument;
  46.     property OnOpenDocument : TNotifyEvent read FOnOpenDocument write FOnOpenDocument;
  47.     property OnCloseDocument : TNotifyEvent read FOnCloseDocument write FOnCloseDocument;
  48.   end;
  49.  
  50.   TWordObject = class
  51.   private
  52.     FWordApp : _Application;
  53.     FEventSink : TWordEventSink;
  54.     function GetCaption : String;
  55.     procedure SetCaption(Value : String);
  56.     function GetVisible : Boolean;
  57.     procedure SetVisible(Value : Boolean);
  58.     function GetOnQuit : TNotifyEvent;
  59.     procedure SetOnQuit(Value : TNotifyEvent);
  60.     function GetOnDocumentChange : TNotifyEvent;
  61.     procedure SetOnDocumentChange(Value : TNotifyEvent);
  62.     function GetOnNewDocument: TNotifyEvent;
  63.     procedure SetOnNewDocument(Value : TNotifyEvent);
  64.     function GetOnOpenDocument: TNotifyEvent;
  65.     procedure SetOnOpenDocument(Value : TNotifyEvent);
  66.     function GetOnCloseDocument: TNotifyEvent;
  67.     procedure SetOnCloseDocument(Value : TNotifyEvent);
  68.   public
  69.     constructor Create;
  70.     destructor Destroy; override;
  71.     procedure NewDoc(Template : String);
  72.     procedure CloseDoc;
  73.     procedure InsertText(Text : String);
  74.     procedure Print;
  75.     procedure SaveAs(Filename : String);
  76.   published
  77.     property Application : _Application read FWordApp;
  78.     property Caption : String read GetCaption write SetCaption;
  79.     property Visible : Boolean read GetVisible write SetVisible;
  80.     property OnQuit : TNotifyEvent read GetOnQuit write SetOnQuit;
  81.     property OnDocumentChange : TNotifyEvent read GetOnDocumentChange write SetOnDocumentChange;
  82.     property OnNewDocument : TNotifyEvent read GetOnNewDocument write SetOnNewDocument;
  83.     property OnOpenDocument : TNotifyEvent read GetOnOpenDocument write SetOnOpenDocument;
  84.     property OnCloseDocument : TNotifyEvent read GetOnCloseDocument write SetOnCloseDocument;
  85.   end;
  86.  
  87. implementation
  88.  
  89. uses
  90.   ComObj;
  91.  
  92. { TWordEventSink implementation }
  93.  
  94. constructor TWordEventSink.Create(AnOwner : TObject; AnAppDispatch: IDispatch; const AnAppDispIntfIID, ADocDispIntfIID: TGUID);
  95. begin
  96.   inherited Create;
  97.  
  98.   FOwner := AnOwner;
  99.   FAppDispIntfIID := AnAppDispIntfIID;
  100.   FDocDispIntfIID := ADocDispIntfIID;
  101.   FAppDispatch := AnAppDispatch;
  102.  
  103.   // Hook the sink up to the automation server (Word97)
  104.   InterfaceConnect(FAppDispatch,FAppDispIntfIID,Self,FAppConnection);
  105. end;
  106.  
  107. destructor TWordEventSink.Destroy;
  108. begin
  109.   // Unhook the sink from the automation server (Word97)
  110.   InterfaceDisconnect(FAppDispatch,FAppDispIntfIID,FAppConnection);
  111.  
  112.   inherited Destroy;
  113. end;
  114.  
  115. function TWordEventSink.QueryInterface(const IID: TGUID; out Obj): HRESULT;
  116. begin
  117.   // We need to return the two event interfaces when they're asked for
  118.   Result := E_NOINTERFACE;
  119.   if GetInterface(IID,Obj) then
  120.     Result := S_OK;
  121.   if IsEqualGUID(IID,FAppDispIntfIID) and GetInterface(IDispatch,Obj) then
  122.     Result := S_OK;
  123.   if IsEqualGUID(IID,FDocDispIntfIID) and GetInterface(IDispatch,Obj) then
  124.     Result := S_OK;
  125. end;
  126.  
  127. function TWordEventSink._AddRef: Integer;
  128. begin
  129. // Skeleton implementation
  130.   Result := 2;
  131. end;
  132.  
  133. function TWordEventSink._Release: Integer;
  134. begin
  135. // Skeleton implementation
  136.   Result := 1;
  137. end;
  138.  
  139. function TWordEventSink.GetTypeInfoCount(out Count: Integer): HRESULT;
  140. begin
  141. // Skeleton implementation
  142.   Count  := 0;
  143.   Result := S_OK;
  144. end;
  145.  
  146. function TWordEventSink.GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HRESULT;
  147. begin
  148. // Skeleton implementation
  149.   Result := E_NOTIMPL;
  150. end;
  151.  
  152. function TWordEventSink.GetIDsOfNames(const IID: TGUID; Names: Pointer;
  153.   NameCount, LocaleID: Integer; DispIDs: Pointer): HRESULT;
  154. begin
  155. // Skeleton implementation
  156.   Result := E_NOTIMPL;
  157. end;
  158.  
  159. function TWordEventSink.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
  160.   Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HRESULT;
  161. begin
  162.   // Fire the different event handlers when
  163.   // the different event methods are invoked
  164.   case DispID of
  165.     2 : if Assigned(FOnQuit) then
  166.           FOnQuit(FOwner);
  167.     3 : begin
  168.           if Assigned(FOnDocumentChange) then
  169.             FOnDocumentChange(FOwner);
  170.           // When we see a document change, we also need to disconnect the
  171.           // sink from the old document, and hook it up to the new document
  172.           InterfaceDisconnect(FDocDispatch,FDocDispIntfIID,FDocConnection);
  173.           try
  174.             FDocDispatch := _Application(FAppDispatch).ActiveDocument;
  175.             InterfaceConnect(FDocDispatch,FDocDispIntfIID,Self,FDocConnection);
  176.           except;
  177.           end;
  178.         end;
  179.     4 : if Assigned(FOnNewDocument) then
  180.           FOnNewDocument(FOwner);
  181.     5 : if Assigned(FOnOpenDocument) then
  182.           FOnOpenDocument(FOwner);
  183.     6 : if Assigned(FOnCloseDocument) then
  184.           FOnCloseDocument(FOwner);
  185.   end;
  186.  
  187.   Result := S_OK;
  188. end;
  189.  
  190. { TWordObject implementation }
  191.  
  192. constructor TWordObject.Create;
  193. begin
  194.   // Fire off Word97 and create the event sink
  195.   FWordApp := CoApplication_.Create;
  196.   FEventSink := TWordEventSink.Create(Self,FWordApp,ApplicationEvents,DocumentEvents);
  197. end;
  198.  
  199. destructor TWordObject.Destroy;
  200. var
  201.   SaveChanges,
  202.   OriginalFormat,
  203.   RouteDocument  : OleVariant;
  204. begin
  205.   SaveChanges := WdDoNotSaveChanges;
  206.   OriginalFormat := UnAssigned;
  207.   RouteDocument := UnAssigned;
  208.   try
  209.     FWordApp.Quit(SaveChanges,OriginalFormat,RouteDocument);
  210.   except
  211.   end;
  212.   FEventSink := nil;
  213.   inherited Destroy;
  214. end;
  215.  
  216. function TWordObject.GetVisible : Boolean;
  217. begin
  218.   Result := FWordApp.Visible;
  219. end;
  220.  
  221. procedure TWordObject.SetCaption(Value : String);
  222. begin
  223.   FWordApp.Caption := Value;
  224. end;
  225.  
  226. function TWordObject.GetCaption : String;
  227. begin
  228.   Result := FWordApp.Caption;
  229. end;
  230.  
  231. procedure TWordObject.SetVisible(Value : Boolean);
  232. begin
  233.   FWordApp.Visible := Value;
  234. end;
  235.  
  236. function TWordObject.GetOnQuit : TNotifyEvent;
  237. begin
  238.   Result := FEventSink.OnQuit;
  239. end;
  240.  
  241. procedure TWordObject.SetOnQuit(Value : TNotifyEvent);
  242. begin
  243.   FEventSink.OnQuit := Value;
  244. end;
  245.  
  246. function TWordObject.GetOnDocumentChange : TNotifyEvent;
  247. begin
  248.   Result := FEventSink.OnDocumentChange;
  249. end;
  250.  
  251. procedure TWordObject.SetOnDocumentChange(Value : TNotifyEvent);
  252. begin
  253.   FEventSink.OnDocumentChange := Value;
  254. end;
  255.  
  256. function TWordObject.GetOnNewDocument : TNotifyEvent;
  257. begin
  258.   Result := FEventSink.OnNewDocument;
  259. end;
  260.  
  261. procedure TWordObject.SetOnNewDocument(Value : TNotifyEvent);
  262. begin
  263.   FEventSink.OnNewDocument := Value;
  264. end;
  265.  
  266. function TWordObject.GetOnOpenDocument : TNotifyEvent;
  267. begin
  268.   Result := FEventSink.OnOpenDocument;
  269. end;
  270.  
  271. procedure TWordObject.SetOnOpenDocument(Value : TNotifyEvent);
  272. begin
  273.   FEventSink.OnOpenDocument := Value;
  274. end;
  275.  
  276. function TWordObject.GetOnCloseDocument : TNotifyEvent;
  277. begin
  278.   Result := FEventSink.OnCloseDocument;
  279. end;
  280.  
  281. procedure TWordObject.SetOnCloseDocument(Value : TNotifyEvent);
  282. begin
  283.   FEventSink.OnCloseDocument := Value;
  284. end;
  285.  
  286. procedure TWordObject.InsertText(Text : String);
  287. begin
  288.   FWordApp.Selection.TypeText(Text);
  289. end;
  290.  
  291. procedure TWordObject.NewDoc(Template : String);
  292. var
  293.   DocTemplate,
  294.   NewTemplate : OleVariant;
  295. begin
  296.   DocTemplate := Template;
  297.   NewTemplate := False;
  298.   FWordApp.Documents.Add(DocTemplate,NewTemplate);
  299. end;
  300.  
  301. procedure TWordObject.CloseDoc;
  302. var
  303.   SaveChanges,
  304.   OriginalFormat,
  305.   RouteDocument  : OleVariant;
  306. begin
  307.   SaveChanges := WdDoNotSaveChanges;
  308.   OriginalFormat := UnAssigned;
  309.   RouteDocument := UnAssigned;
  310.   FWordApp.ActiveDocument.Close(SaveChanges,OriginalFormat,RouteDocument);
  311. end;
  312.  
  313. procedure TWordObject.Print;
  314. begin
  315.   OleVariant(FWordApp).PrintOut;
  316. end;
  317.  
  318. procedure TWordObject.SaveAs(Filename : String);
  319. begin
  320.   OleVariant(FWordApp).ActiveDocument.SaveAs(FileName);
  321. end;
  322.  
  323. end.
  324.  
  325.