Solved

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

Posted on 2004-10-29
172 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
Question by:marioway
    10 Comments
     
    LVL 17

    Expert Comment

    by:geobul
    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
    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
    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
     
    LVL 17

    Expert Comment

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

    Author Comment

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

    Marioway
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now