Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-29
10
Medium Priority
?
177 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:marioway
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 12443072
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
 

Author Comment

by:marioway
ID: 12443129
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
 
LVL 17

Expert Comment

by:geobul
ID: 12443351
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Expert Comment

by:geobul
ID: 12443358
And delete the file after loading: DeleteFile('c:\temp\foo.rtf');
0
 

Author Comment

by:marioway
ID: 12445301
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
 
LVL 17

Expert Comment

by:geobul
ID: 12447359
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
 

Author Comment

by:marioway
ID: 12447930
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
 

Author Comment

by:marioway
ID: 12448255
Hello, I'm leaving now for 1 week on vacation. I'll be back the 8th of november.

See you then

marioway
0
 
LVL 17

Accepted Solution

by:
geobul earned 2000 total points
ID: 12502280
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
 

Author Comment

by:marioway
ID: 12518081
Hi Geo, I'm back in town. Tried your solution and it works fine. Thanks a lot !!!

Marioway
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question