Manuel Lopez-Michelone
asked on
select a word from a Word Document to process using Delphi
Hi guys,
Im working in a spanish check speller program written in delphi. Im using OLE automation to create an instance of Word application. I can load a document from Delphi and also, I can insert text from my delphi application to Word.
So, having a document loaded in Word, I want to get word by word (from Word document to Delphi), so I can process it with my check spell method and then return back corrected to the word document. I think I have to use some sort of select text method but I cant find how to use it...
Any expert around here to help me? I will give the points to a working example...
best regards,
Manuel Lopez (lopem)
Ps.This is the unit I am using:
unit WordAuto;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
LaunchButton: TButton;
CloseButton: TButton;
Memo1: TMemo;
NewDocButton: TButton;
TypeTextButton: TButton;
WordText: TEdit;
Label1: TLabel;
procedure LaunchButtonClick(Sender: TObject);
procedure CloseButtonClick(Sender: TObject);
procedure NewDocButtonClick(Sender: TObject);
procedure TypeTextButtonClick(Sender : TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private Declarations }
procedure AppQuit(Sender: TObject);
procedure AppDocumentChange(Sender: TObject);
procedure DocNew(Sender: TObject);
procedure DocOpen(Sender: TObject);
procedure DocClose(Sender: TObject);
public
{ Public Declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses
AutoImpl;
var
WordObject : TWordObject;
procedure TForm1.LaunchButtonClick(S ender: TObject);
begin
if not Assigned(WordObject) then
begin
WordObject := TWordObject.Create;
with WordObject do
begin
Caption := 'Delphi is RAD!';
Visible := True;
OnQuit := AppQuit;
OnDocumentChange := AppDocumentChange;
OnNewDocument := DocNew;
OnOpenDocument := DocOpen;
OnCloseDocument := DocClose;
end;
LaunchButton.Enabled := False;
CloseButton.Enabled := True;
NewDocButton.Enabled := True;
end;
end;
procedure TForm1.CloseButtonClick(Se nder: TObject);
begin
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
procedure TForm1.AppDocumentChange(S ender: TObject);
begin
Memo1.Lines.Add('Document changed');
TypeTextButton.Enabled := WordObject.Application.Doc uments.Cou nt > 0;
end;
procedure TForm1.AppQuit(Sender: TObject);
begin
Memo1.Lines.Add('Quitting Word');
WordObject.Free;
WordObject := nil;
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
end;
procedure TForm1.DocNew(Sender: TObject);
begin
Memo1.Lines.Add('New document');
end;
procedure TForm1.DocOpen(Sender: TObject);
begin
Memo1.Lines.Add('Document opened');
end;
procedure TForm1.DocClose(Sender: TObject);
begin
Memo1.Lines.Add('Document closed');
end;
procedure TForm1.NewDocButtonClick(S ender: TObject);
begin
try
WordObject.NewDoc('');
except
ShowMessage('It seems like somebody killed Word and didn''t tell me about it...');
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
end;
procedure TForm1.TypeTextButtonClick (Sender: TObject);
begin
WordObject.InsertText(Word Text.Text) ;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(WordObject) then begin
WordObject.Free;
WordObject := nil;
end;
end;
end.
But... i dont know how i can get word by word from Word document to Delphi.
Im working in a spanish check speller program written in delphi. Im using OLE automation to create an instance of Word application. I can load a document from Delphi and also, I can insert text from my delphi application to Word.
So, having a document loaded in Word, I want to get word by word (from Word document to Delphi), so I can process it with my check spell method and then return back corrected to the word document. I think I have to use some sort of select text method but I cant find how to use it...
Any expert around here to help me? I will give the points to a working example...
best regards,
Manuel Lopez (lopem)
Ps.This is the unit I am using:
unit WordAuto;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
LaunchButton: TButton;
CloseButton: TButton;
Memo1: TMemo;
NewDocButton: TButton;
TypeTextButton: TButton;
WordText: TEdit;
Label1: TLabel;
procedure LaunchButtonClick(Sender: TObject);
procedure CloseButtonClick(Sender: TObject);
procedure NewDocButtonClick(Sender: TObject);
procedure TypeTextButtonClick(Sender
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private Declarations }
procedure AppQuit(Sender: TObject);
procedure AppDocumentChange(Sender: TObject);
procedure DocNew(Sender: TObject);
procedure DocOpen(Sender: TObject);
procedure DocClose(Sender: TObject);
public
{ Public Declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses
AutoImpl;
var
WordObject : TWordObject;
procedure TForm1.LaunchButtonClick(S
begin
if not Assigned(WordObject) then
begin
WordObject := TWordObject.Create;
with WordObject do
begin
Caption := 'Delphi is RAD!';
Visible := True;
OnQuit := AppQuit;
OnDocumentChange := AppDocumentChange;
OnNewDocument := DocNew;
OnOpenDocument := DocOpen;
OnCloseDocument := DocClose;
end;
LaunchButton.Enabled := False;
CloseButton.Enabled := True;
NewDocButton.Enabled := True;
end;
end;
procedure TForm1.CloseButtonClick(Se
begin
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
procedure TForm1.AppDocumentChange(S
begin
Memo1.Lines.Add('Document changed');
TypeTextButton.Enabled := WordObject.Application.Doc
end;
procedure TForm1.AppQuit(Sender: TObject);
begin
Memo1.Lines.Add('Quitting Word');
WordObject.Free;
WordObject := nil;
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
end;
procedure TForm1.DocNew(Sender: TObject);
begin
Memo1.Lines.Add('New document');
end;
procedure TForm1.DocOpen(Sender: TObject);
begin
Memo1.Lines.Add('Document opened');
end;
procedure TForm1.DocClose(Sender: TObject);
begin
Memo1.Lines.Add('Document closed');
end;
procedure TForm1.NewDocButtonClick(S
begin
try
WordObject.NewDoc('');
except
ShowMessage('It seems like somebody killed Word and didn''t tell me about it...');
if Assigned(WordObject) then begin
LaunchButton.Enabled := True;
CloseButton.Enabled := False;
NewDocButton.Enabled := False;
TypeTextButton.Enabled := False;
WordObject.Free;
WordObject := nil;
end;
end;
end;
procedure TForm1.TypeTextButtonClick
begin
WordObject.InsertText(Word
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Assigned(WordObject) then begin
WordObject.Free;
WordObject := nil;
end;
end;
end.
But... i dont know how i can get word by word from Word document to Delphi.
ASKER
Hi Robert,
How you can write a WLL? I dont really know. In fact, I just want to know how to get an item (a word) from a Word Document to process it in my Delphi application and returnr the item (modified) to the Word document.
Best regards,
Manuel Lopez (lopem)
How you can write a WLL? I dont really know. In fact, I just want to know how to get an item (a word) from a Word Document to process it in my Delphi application and returnr the item (modified) to the Word document.
Best regards,
Manuel Lopez (lopem)
ASKER
50 more points!
Any taker?
best wishes
Manuel Lopez (lopem)
Any taker?
best wishes
Manuel Lopez (lopem)
My Word knowledge is a bit rusty (Word 97). A WLL is nothing but a DLL with some functions with certain names exported. It is placed in a directory of the Word installation where Word loads it and calls the functions. The WLL stays loaded as long as word runs. You can export your own functions from the WLL and bind them in Word to new menu entries.
From the WLL you use COM to automate Word. The complete VBA of Word is available through COM so you implement something which is almost a VBA macro. The VBA help of Word is normally on the Word installation CD, but is not installed per default.
Please nag me and i will send you some WLL source code (in C i am afraid).
From the WLL you use COM to automate Word. The complete VBA of Word is available through COM so you implement something which is almost a VBA macro. The VBA help of Word is normally on the Word installation CD, but is not installed per default.
Please nag me and i will send you some WLL source code (in C i am afraid).
ASKER
Robert,
please send me your c code. Let me see if i can figure out how to do that! Thanks in advance... My email is morsa@la-morsa.com
best regards
Manuel Lopez (lopem)
please send me your c code. Let me see if i can figure out how to do that! Thanks in advance... My email is morsa@la-morsa.com
best regards
Manuel Lopez (lopem)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Robert,
I appreciate your effort. Where are you from by the way? Are you from Germany?
Anyway, I will looking forward for your code...
best regards,
Manuel Lopez (lopem)
I appreciate your effort. Where are you from by the way? Are you from Germany?
Anyway, I will looking forward for your code...
best regards,
Manuel Lopez (lopem)
ASKER
Thanks Robert, It seems I can work around your code...
best regards
Manuel López (lopem)
best regards
Manuel López (lopem)
You can write procedures which can be easily assigned to Word menu items.
From there on it is mainly calling VBA. Install the VBA help of Word.
Do NOT use the index properties of the collections of Word. They are SLOW.
I made such an Addin for Word 97 and Word 97 was buggy as hell.