• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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
0
marioway
Asked:
marioway
  • 5
  • 5
1 Solution
 
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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
 
mariowayAuthor Commented:
Hi Geo, I'm back in town. Tried your solution and it works fine. Thanks a lot !!!

Marioway
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now