Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2005, 07:48 AM
jfbakker jfbakker is offline
Junior Member
 
Join Date: May 2005
Posts: 4
Default Copy text from a Word document and paste it into the clipboard

Dear friends,

I need urgent help with the following problem. I need a Delphi application to do the following:

1. Select a MS Word document.
2. Open it and copy all its content.
3. Paste the copied content into e.g. a TMemo component, a text file or in general, to the clipboard.

I know now how to implement the first two steps, it is the last one that keeps on producing me headaches.

Anybody here who can help me with this?

Thanks in advance,
FRANS BAKKER
Reply With Quote
  #2  
Old 05-23-2005, 08:54 AM
douglas douglas is offline
Senior Member
 
Join Date: Jul 2001
Posts: 7,695
Default RE: Copy text from a Word document and paste it into the clipboard

You say you know how to do the first two steps?

So you have accomplished this task?

2. Open it and copy all its content.

If so, is this contents then in the Clipboard? Memory?

UnSysAPps
Reply With Quote
  #3  
Old 05-23-2005, 09:15 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 4,832
Default RE: Copy text from a Word document and paste it into the clipboard

Here is the answer:

USES ComObj, Clipbrd;
.................................................. .
.................................................. .
.................................................. .
var
oWord : OLEVariant;
begin
oWord := CreateOleObject('Word.Application');

//If you do not want to see word, set it to false.
oWord.Visible := False;

//Open the word document.
oWord.Documents.Open('C:\myDocument.doc');

//Select all.
oWord.ActiveDocument.Select;

//Copy to Clipboard.
oWord.ActiveDocument.Range.Copy;

//Close the document.
oWord.ActiveDocument.Close;

//Quit word.
oWord.Quit;

//Free.
oWord := unassigned;

//Now it is time to paste.
Memo1.PasteFromClipboard;


Regards,
Abdulaziz Jasser
Reply With Quote
  #4  
Old 05-23-2005, 09:51 AM
jfbakker jfbakker is offline
Junior Member
 
Join Date: May 2005
Posts: 4
Default RE: Copy text from a Word document and paste it into the clipboard

That is exactly what I don't know.

Here is the source code I have developed so far:

================================================== ==========

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl, Word2000, OleServer;

type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
FullAttachedFileName: String;
begin
OpenDialog1.Filter := 'Word documents (*.doc)|*.doc|Excel spreadsheets (*.xls)|*.xls';
if OpenDialog1.Execute then
begin
FullAttachedFileName := OpenDialog1.FileName;
Edit1.Text := FullAttachedFileName;
ChDir('C:\');
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
WordDocName,
WordDocDir,
WordDocDest,
WordDocSelContent: String;
WordDocDrive: Char;
WordDoc1: TWordDocument;
WordDocName1: OleVariant;
begin
if not (Edit1.Text = '') then
begin
Memo1.Text := '';
ProcessPath(Edit1.Text,WordDocDrive,WordDocDir,Wor dDocName);
WordDocName1 := Edit1.Text;
WordDoc1 := TWordDocument.Create(self);
WordDoc1.Connect;
WordDoc1.ActiveWindow.Selection.WholeStory;
WordDoc1.ActiveWindow.Selection.Copy;
WordDoc1.Close;
WordDoc1.Disconnect;
WordDoc1.Free;
end;
end;

================================================== ==========

TForm1.Button1Click is a simple procedure to select a Word document through a dialog box.

TForm1.Button2Click actually is the procedure that is to open the word doc, select all its content, copy it and then paste it on a TMemo component.

The commands
WordDoc1.ActiveWindow.Selection.WholeStory;
WordDoc1.ActiveWindow.Selection.Copy;
are easily derived from the VB source code that is generated when creating a macro in MS Word.

Thanks for your help,
FRANS BAKKER
Reply With Quote
  #5  
Old 05-23-2005, 10:10 AM
jfbakker jfbakker is offline
Junior Member
 
Join Date: May 2005
Posts: 4
Default RE: Copy text from a Word document and paste it into the clipboard

Dear Jasser,

ĦThat works! Thank you very much indeed.
Something else, żis there any documentation available on the net on how to deal with these MS Office components in Delphi programs?

Once again, thank you very much.
FRANS BAKKER
Reply With Quote
  #6  
Old 05-23-2005, 10:55 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 4,832
Default RE: Copy text from a Word document and paste it into the clipboard

Sorry Frans, but I got it all from one of my old applications. However, there is no one-place to go to find documentation about OLE and Office automation. Try Microsoft and Borland. You have to dig there.

Regards,
Abdulaziz Jasser
Reply With Quote
  #7  
Old 05-23-2005, 11:13 AM
agent86 agent86 is offline
Senior Member
 
Join Date: Jan 2002
Posts: 1,191
Default RE: Copy text from a Word document and paste it into the clipboard

Here is a site that may of interest.

http://www.djpate.freeserve.co.uk/Automation.htm

Donald Adams
Agent86
Reply With Quote
  #8  
Old 05-24-2005, 12:59 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 4,832
Default RE: Copy text from a Word document and paste it into the clipboard

Hi BAKKER, I think you forget to press the red button at top-right of the screen

Regards,
Abdulaziz Jasser
Reply With Quote
  #9  
Old 05-24-2005, 08:44 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 4,832
Default RE: Copy text from a Word document and paste it into the clipboard

This is not fair, I replied to your main question with a complete code and now you give the point to another person. Thanks for showing your self.

Regards,
Abdulaziz Jasser
Reply With Quote
  #10  
Old 05-24-2005, 09:19 AM
jfbakker jfbakker is offline
Junior Member
 
Join Date: May 2005
Posts: 4
Default RE: Copy text from a Word document and paste it into the clipboard

Dear Jasser,

Sorry! I did not mean to do that! Since it is the first time I used this forum I did not know what that link was meant for. I was already surprised not seeing it again on your reply when I wanted to click the link "Did this answer satisfy your question". Simply I was not aware that it was for giving credits to repliers.

Of course the point is for you!

Please, accept my apologies.
FRANS BAKKER
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 05:04 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.