http://www.emu.dk/gym/fag/dl/forening/bladet/nr78/msword.htmunit testunit2; // A first attempt at automating Word... // Copyright Huw Collingbourne 2001interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComObj, StdCtrls; // NOTE: You must use ComObjtype TForm1 = class(TForm) RunBtn: TButton; QuitBtn: TButton; WriteTextBtn: TButton; GetStatsBtn: TButton; Memo1: TMemo; procedure RunBtnClick(Sender: TObject); procedure QuitBtnClick(Sender: TObject); procedure WriteTextBtnClick(Sender: TObject); procedure GetStatsBtnClick(Sender: TObject); private { Private declarations } public { Public declarations } MyWordApp, newDoc : Variant; end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.RunBtnClick(Sender: TObject); begin // load Word if VarIsEmpty(MyWordApp) then begin MyWordApp := CreateOleObject('Word.Application'); // run word MyWordApp.Visible := true; // show it onscreen end else ShowMessage( 'Word is already running!' ); end;procedure TForm1.QuitBtnClick(Sender: TObject); begin if not VarIsEmpty(MyWordApp) then begin MyWordApp.Quit; MyWordApp := UnAssigned; end else ShowMessage( 'You must open an instance of Word before you can close it!' ); end;procedure TForm1.WriteTextBtnClick(Sender: TObject); var i : integer; begin if VarIsEmpty(MyWordApp) then ShowMessage('You must start a new instance of Word before trying to add text!') else begin newDoc := MyWordApp.Documents.Add; newDoc.Content.Font.Name := 'Arial'; MyWordApp.Selection.TypeText('This is Hello from '); MyWordApp.Selection.Font.Name := 'Times New Roman'; MyWordApp.Selection.Font.Size := 20; MyWordApp.Selection.Font.Bold := True; MyWordApp.Selection.TypeText('Delphi'); for i := 1 to 10 do begin MyWordApp.Selection.Font.Size := MyWordApp.Selection.Font.Size + 1; MyWordApp.Selection.TypeText('!'); end; end; End;procedure TForm1.GetStatsBtnClick(Sender: TObject); const MywdDialogToolsWordCount = 228; var numWords : integer; temp : Variant; begin if VarIsEmpty(MyWordApp) then ShowMessage( 'You need to create an instance of Word!' ) else if VarIsEmpty(newDoc) then ShowMessage( 'You need to add a document! ') else begin ShowMessage('The document "' + newDoc.Name + '" contains '+ IntToStr(newDoc.Words.Count) + ' words.'); temp := MyWordApp.Dialogs.Item(MywdDialogToolsWordCount); // Execute the dialog box in order to refresh its data. temp.Execute; ShowMessage( 'That''s ' + IntToStr(numWords) + ' not counting punctuation.' ); end; end; end.
// A first attempt at automating Word...
// Copyright Huw Collingbourne 2001interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComObj, StdCtrls; // NOTE: You must use ComObjtype
TForm1 = class(TForm)
RunBtn: TButton;
QuitBtn: TButton;
WriteTextBtn: TButton;
GetStatsBtn: TButton;
Memo1: TMemo;
procedure RunBtnClick(Sender: TObject);
procedure QuitBtnClick(Sender: TObject);
procedure WriteTextBtnClick(Sender: TObject);
procedure GetStatsBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyWordApp, newDoc : Variant;
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.RunBtnClick(Sender: TObject);
begin
// load Word
if VarIsEmpty(MyWordApp) then
begin
MyWordApp := CreateOleObject('Word.Application'); // run word
MyWordApp.Visible := true; // show it onscreen
end
else
ShowMessage( 'Word is already running!' );
end;procedure TForm1.QuitBtnClick(Sender: TObject);
begin
if not VarIsEmpty(MyWordApp) then
begin
MyWordApp.Quit;
MyWordApp := UnAssigned;
end
else
ShowMessage( 'You must open an instance of Word before you can close it!' );
end;procedure TForm1.WriteTextBtnClick(Sender: TObject);
var
i : integer;
begin
if VarIsEmpty(MyWordApp) then
ShowMessage('You must start a new instance of Word before trying to add text!')
else
begin
newDoc := MyWordApp.Documents.Add;
newDoc.Content.Font.Name := 'Arial';
MyWordApp.Selection.TypeText('This is Hello from ');
MyWordApp.Selection.Font.Name := 'Times New Roman';
MyWordApp.Selection.Font.Size := 20;
MyWordApp.Selection.Font.Bold := True;
MyWordApp.Selection.TypeText('Delphi');
for i := 1 to 10 do
begin
MyWordApp.Selection.Font.Size := MyWordApp.Selection.Font.Size + 1;
MyWordApp.Selection.TypeText('!');
end;
end;
End;procedure TForm1.GetStatsBtnClick(Sender: TObject);
const
MywdDialogToolsWordCount = 228;
var
numWords : integer;
temp : Variant;
begin
if VarIsEmpty(MyWordApp) then
ShowMessage( 'You need to create an instance of Word!' )
else if VarIsEmpty(newDoc) then
ShowMessage( 'You need to add a document! ')
else
begin
ShowMessage('The document "' + newDoc.Name + '" contains '+ IntToStr(newDoc.Words.Count) + ' words.');
temp := MyWordApp.Dialogs.Item(MywdDialogToolsWordCount);
// Execute the dialog box in order to refresh its data.
temp.Execute;
ShowMessage( 'That''s ' + IntToStr(numWords) + ' not counting punctuation.' );
end;
end;
end.
temp := MyWordApp.Dialogs.Item(MywdDialogToolsWordCount);