How to make a new document in WORD and import the FORMATTED TEXT into a DBRichtEdit

Hi

How do I make a new document in WORD from within a Delphi application and insert the formatted text into a DBRichtEdit.

thx
Marioway
mariowayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

geobulCommented:
Hi,

>How do I make a new document in WORD from within a Delphi application

uses
 ComObj;

procedure TForm1.Button1Click(Sender: TObject);
var
  WordApp, WordDoc: OLEVariant;
begin
 WordApp := CreateOleObject('Word.Application');
 WordApp.Visible := True;
 WordApp.Caption:='Winword From Delphi';
 // to create new doc
 WordDoc:=WordApp.Documents.Add;
 WordDoc.Paragraphs.Item(1).Range.Bold :=True;
 WordDoc.Paragraphs.Item(1).Range.Font.Size :=30;
 WordDoc.Paragraphs.Item(1).Range.Font.Name:='Tahoma';
 WordDoc.Paragraphs.Item(1).Range.Text:= 'Welcom To Delphi World';
 // save the doc
 WordDoc.SaveAs('c:\mydoc.doc');
 // print the doc
 WordDoc.PrintOut;
 WordDoc.Close; // closes the current document
 WordApp.Quit; // exits MS Word
end;

>and insert the formatted text into a DBRichtEdit.

Would you explain it a little bit more, please?

Regards, Geo
0
mariowayAuthor Commented:
Hi,

I'm making an invoice program, and on the invoices, sometimes a lot of text has to be added. I'm using a formatted memofield for this purpose. So I would like to call Word from within that application an insert the text from the worddoc after closing down Word.

Marioway
0
geobulCommented:
Hi,

One way is to copy the word document in the clipboard and paste it to the RichEdit. I don't like this way.

Another way is to save the word document somewhere as an .RTF file and load it to the RichEdit using:
RichEdit1.Lines.LoadFromFile('c:\temp\foo.rtf');

Regards, Geo
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

geobulCommented:
And delete the file after loading: DeleteFile('c:\temp\foo.rtf');
0
mariowayAuthor Commented:
Hi,

When i use the code above, Word creates a new doc, saves and exits. Could you give me the code for this

- open Word and append a document which one can edit;
- save that doc as an RTF-file in a specified directory
- after closing word loading the file into the (DB)RichEdit;
- delete the file at the end

I would be very greatfull

marioway
0
geobulCommented:
Hi,

>- open Word and append a document which one can edit;
I can't understand that. Sorry. Would you explain it more, please?

>- save that doc as an RTF-file in a specified directory
>- after closing word loading the file into the (DB)RichEdit;
>- delete the file at the end
Sure. Here it is:

  ...
  WordDoc.SaveAs('c:\temp\foo.rtf', 6); \\ 6 means wdFormatRTF constant as a second parameter in WordDocument.SaveAs method
  WordDoc.Close; // closes the current document
  WordApp.Quit; // exits MS Word
  RichEdit1.Lines.LoadFromFile('c:\temp\foo.rtf');
  DeleteFile('c:\temp\foo.rtf');
end;

Regards, Geo
0
mariowayAuthor Commented:
Hi,

Well, when I use the code you sent me first (on top), Word opens, creates a new doc, with the text 'Welcom To Delphi World', prints it out and closes Word, without giving me the opportunity to change the text first.

Marioway
0
mariowayAuthor Commented:
Hello, I'm leaving now for 1 week on vacation. I'll be back the 8th of november.

See you then

marioway
0
geobulCommented:
Hi,

I have time now and will try to explain what I'd do:

I would use two buttons on the form:
- first one is for creating an empty document and showing that document for editing;
- the second button will be for transferring the RTF in a DBRichEdit (also on the same form in this example).

In order to implement that I will need WordApp and WordDoc variables to be accessible by the code in both OnClick events, so these variables will become unit variables. Here is the code:

type
  TForm1 = class(TForm)
    btnCreateNewDoc: TButton;
    btnCloseDoc: TButton;
    DBRichEdit1: TDBRichEdit;
    procedure btnCreateNewDocClick(Sender: TObject);
    procedure btnCloseDocClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses ComObj;

var
  WordApp, WordDoc: OLEVariant;

procedure TForm1.btnCreateNewDocClick(Sender: TObject);
begin
  try
    WordApp := CreateOleObject('Word.Application');
  except
    ShowMessage('Cannot find MS Word');
    exit;
  end;

  try
    WordDoc:=WordApp.Documents.Add;
  except
    ShowMessage('Cannot create a new document');
    WordApp.Quit;
    exit;
  end;

  WordApp.Visible := True;
end;

procedure TForm1.btnCloseDocClick(Sender: TObject);
begin
  try
    WordDoc.SaveAs('c:\temp\foo.rtf', 6); // 6 means wdFormatRTF constant as a second parameter in WordDocument.SaveAs method
    // you may remove the next two lines if you don't want to close Word
    WordDoc.Close; // closes the current document
    WordApp.Quit; // exits MS Word
  except
    ShowMessage('Word document not found');
    exit;
  end;

  try
    DBRichEdit1.Lines.LoadFromFile('c:\temp\foo.rtf');
    DeleteFile('c:\temp\foo.rtf');
  except
    ShowMessage('Temp file not found');
  end;
end;

Regards, Geo
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mariowayAuthor Commented:
Hi Geo, I'm back in town. Tried your solution and it works fine. Thanks a lot !!!

Marioway
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.