Solved

Delphi and MS Word

Posted on 2006-11-15
5
188 Views
Last Modified: 2010-04-05
Hi all,

I need to insert lines, tabs and spaces in a word doc.
I want to do that using TWordApllication component.
Example:

    Line1
        Line2
            Line3

   Line4
   Line5

and so on...

regards
0
Comment
Question by:paulo_psa
5 Comments
 
LVL 19

Accepted Solution

by:
Limbeck earned 63 total points
ID: 17953940
0
 
LVL 9

Assisted Solution

by:bernani
bernani earned 62 total points
ID: 17960604
Hi,

You can use this code which will open Word, create a new doc, insert lines and tabs and finally show word with a new document containing this result:


Line 1
      Line 2
            Line 3
Line 4
Line 5



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComObj, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
   
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1            : TForm1;
  Word             : OLEVariant;
  Sel, Doc         : Variant;

const
vbTab = Chr(9);

implementation

{$R *.DFM}

procedure Return;
begin
Sel.TypeParagraph;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
  Word := CreateOleObject('Word.Application');
  except
  ShowMessage('Cannot start Word !');
  Exit;
  end;

// Word will only be visible when the text is inserted
Doc:=Word.Documents.Add;
Sel:=Word.Selection;

Sel.Font.Name     := 'Times New Roman';
Sel.Font.Size     := 12;
Sel.ParagraphFormat.Alignment := 0; //left aligned

Sel.TypeText('Line 1');
Return;
Sel.TypeText(Text := vbtab + 'Line 2');
Return;
Sel.TypeText(Text := vbtab + vbtab + 'Line 3');
Return;
Sel.TypeText(Text := 'Line 4');
Return;
Sel.TypeText(Text := 'Line 5');
Return;

Word.Visible := True;

end;

end.

_____________________________

Hope this help

PS:

Try to have a look at those pages http://www.djpate.freeserve.co.uk/Automation.htm

You got good infos how to use the compos TWordApplication and others.

See the provided demo: http://www.djpate.freeserve.co.uk/D5Word.zip.

0
 

Expert Comment

by:landscapeEco
ID: 17979353
I think this should be the accepted answer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21133166
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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